-
Notifications
You must be signed in to change notification settings - Fork 161
/
main.go
176 lines (148 loc) · 3.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/chai2010/winsvc"
"github.com/macronut/ghostcp/header"
)
var ServiceMode bool = true
var ScanIPRange string = ""
var ScanSpeed int = 1
var ScanURL string = ""
var ScanTimeout uint = 0
func StartService() {
runtime.GOMAXPROCS(1)
log.SetFlags(log.LstdFlags | log.Lshortfile)
if ghostcp.LogLevel > 0 {
var logFilename string = "ghostcp.log"
logFile, err := os.OpenFile(logFilename, os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
log.Println(err)
return
}
defer logFile.Close()
ghostcp.Logger = log.New(logFile, "\r\n", log.Ldate|log.Ltime|log.Lshortfile)
}
err := ghostcp.LoadConfig()
if err != nil {
if ghostcp.LogLevel > 0 || !ServiceMode {
log.Println(err)
}
return
}
Windir := os.Getenv("WINDIR")
err = ghostcp.LoadHosts(Windir + "\\System32\\drivers\\etc\\hosts")
if err != nil && !ServiceMode {
log.Println(err)
return
}
if ghostcp.LogLevel == 0 && !ServiceMode {
ghostcp.LogLevel = 1
}
if ScanIPRange != "" {
ghostcp.DetectEnable = true
ghostcp.ScanURL = ScanURL
ghostcp.ScanTimeout = ScanTimeout
}
ghostcp.TCPDaemon(":443", false)
ghostcp.TCPDaemon(":80", false)
ghostcp.UDPDaemon(443, false)
ghostcp.TCPRecv(":443", false)
if ghostcp.Forward {
ghostcp.TCPDaemon(":443", true)
ghostcp.TCPDaemon(":80", true)
ghostcp.UDPDaemon(443, true)
ghostcp.TCPRecv(":443", true)
}
if ghostcp.DNS == "" {
ghostcp.DNSRecvDaemon()
} else {
ghostcp.TCPDaemon(ghostcp.DNS, false)
ghostcp.TCPRecv(ghostcp.DNS, false)
ghostcp.DNSDaemon()
}
if ScanIPRange != "" {
go ghostcp.Scan(ScanIPRange, ScanSpeed)
}
fmt.Println("Service Start")
ghostcp.Wait()
}
func StopService() {
arg := []string{"/flushdns"}
cmd := exec.Command("ipconfig", arg...)
d, err := cmd.CombinedOutput()
if err != nil {
log.Println(string(d), err)
}
os.Exit(0)
}
func main() {
serviceName := "GhosTCP"
var flagServiceInstall bool
var flagServiceUninstall bool
var flagServiceStart bool
var flagServiceStop bool
flag.BoolVar(&flagServiceInstall, "install", false, "Install service")
flag.BoolVar(&flagServiceUninstall, "remove", false, "Remove service")
flag.BoolVar(&flagServiceStart, "start", false, "Start service")
flag.BoolVar(&flagServiceStop, "stop", false, "Stop service")
flag.StringVar(&ScanIPRange, "scanip", "", "Scan IP Range")
flag.IntVar(&ScanSpeed, "scanspeed", 1, "Scan Speed")
flag.StringVar(&ScanURL, "scanurl", "", "Scan URL")
flag.UintVar(&ScanTimeout, "scantimeout", 0, "Scan Timeout")
flag.Parse()
appPath, err := winsvc.GetAppPath()
if err != nil {
log.Fatal(err)
}
// install service
if flagServiceInstall {
if err := winsvc.InstallService(appPath, serviceName, ""); err != nil {
log.Fatalf("installService(%s, %s): %v\n", serviceName, "", err)
}
log.Printf("Done\n")
return
}
// remove service
if flagServiceUninstall {
if err := winsvc.RemoveService(serviceName); err != nil {
log.Fatalln("removeService:", err)
}
log.Printf("Done\n")
return
}
// start service
if flagServiceStart {
if err := winsvc.StartService(serviceName); err != nil {
log.Fatalln("startService:", err)
}
log.Printf("Done\n")
return
}
// stop service
if flagServiceStop {
if err := winsvc.StopService(serviceName); err != nil {
log.Fatalln("stopService:", err)
}
log.Printf("Done\n")
return
}
// run as service
if !winsvc.IsAnInteractiveSession() {
log.Println("main:", "runService")
if err := os.Chdir(filepath.Dir(appPath)); err != nil {
log.Fatal(err)
}
if err := winsvc.RunAsService(serviceName, StartService, StopService, false); err != nil {
log.Fatalf("svc.Run: %v\n", err)
}
return
}
ServiceMode = false
StartService()
}