forked from macronut/ghostcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (147 loc) · 3.72 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/Macronut/TCPioneer/header"
"github.com/chai2010/winsvc"
)
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 tcpioneer.LogLevel > 0 {
var logFilename string = "tcpioneer.log"
logFile, err := os.OpenFile(logFilename, os.O_RDWR|os.O_CREATE, 0777)
if err != nil {
log.Println(err)
return
}
defer logFile.Close()
tcpioneer.Logger = log.New(logFile, "\r\n", log.Ldate|log.Ltime|log.Lshortfile)
}
err := tcpioneer.LoadConfig()
if err != nil {
if tcpioneer.LogLevel > 0 || !ServiceMode {
log.Println(err)
}
return
}
Windir := os.Getenv("WINDIR")
err = tcpioneer.LoadHosts(Windir + "\\System32\\drivers\\etc\\hosts")
if err != nil && !ServiceMode {
log.Println(err)
return
}
if tcpioneer.LogLevel == 0 && !ServiceMode {
tcpioneer.LogLevel = 1
}
if ScanIPRange != "" {
tcpioneer.DetectEnable = true
tcpioneer.ScanURL = ScanURL
tcpioneer.ScanTimeout = ScanTimeout
}
tcpioneer.TCPDaemon(":443", false)
tcpioneer.TCPDaemon(":80", false)
tcpioneer.UDPDaemon(443, false)
tcpioneer.TCPRecv(443, false)
if tcpioneer.Forward {
tcpioneer.TCPDaemon(":443", true)
tcpioneer.TCPDaemon(":80", true)
tcpioneer.UDPDaemon(443, true)
tcpioneer.TCPRecv(443, true)
}
if tcpioneer.DNS == "" {
tcpioneer.DNSRecvDaemon()
} else {
tcpioneer.TCPDaemon(tcpioneer.DNS, false)
tcpioneer.DNSDaemon()
}
if ScanIPRange != "" {
go tcpioneer.Scan(ScanIPRange, ScanSpeed)
}
fmt.Println("Service Start")
tcpioneer.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 := "TCPPioneer"
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()
}