-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
148 lines (144 loc) · 3.62 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
package main
import (
"flag"
"fmt"
"github.com/HobaiRiku/wsl2-auto-portproxy/lib/config"
"github.com/HobaiRiku/wsl2-auto-portproxy/lib/proxy"
"github.com/HobaiRiku/wsl2-auto-portproxy/lib/service"
"github.com/HobaiRiku/wsl2-auto-portproxy/lib/storage"
"log"
"os"
"time"
)
var version string
func main() {
// print version
var showVersion bool
flag.BoolVar(&showVersion, "v", false, "show version")
flag.Parse()
if showVersion {
fmt.Println(version)
os.Exit(1)
}
ready := make(chan bool)
// get config interval
go func() {
for {
c, err := config.GetConfig()
if err != nil {
log.Printf("error getting config file: %s", err)
} else {
storage.C = c
}
ready <- true
time.Sleep(time.Second)
}
}()
for {
// wait for a config update interval
<-ready
// get linux's ip
storage.WslIp, _ = service.GetWslIP()
// get all tcp ports in linux now
linuxPorts, err := service.GetLinuxHostPorts()
if err != nil {
log.Printf("GetLinuxHostPorts error: %s, retrying", err)
continue // Skipping current loop is Necessary. Otherwise, running port will be stopped.
}
// change proxy port by config "predefined"
for i, p := range linuxPorts {
for _, predefinedTcpPort := range storage.C.Predefined.Tcp {
if p.Port == predefinedTcpPort.Remote {
linuxPorts[i].ProxyPort = predefinedTcpPort.Local
}
}
}
// filter by config "ignore"
for i := 0; i < len(linuxPorts); {
needToDelete := false
for _, ignorePort := range storage.C.Ignore.Tcp {
if ignorePort == linuxPorts[i].Port {
needToDelete = true
}
}
if needToDelete {
linuxPorts = append(linuxPorts[:i], linuxPorts[i+1:]...)
} else {
i++
}
}
// filter by config "OnlyPredefined"
if storage.C.OnlyPredefined {
for i := 0; i < len(linuxPorts); {
needToDelete := true
for _, predefinedTcpPort := range storage.C.Predefined.Tcp {
if predefinedTcpPort.Remote == linuxPorts[i].Port {
needToDelete = false
}
}
if needToDelete {
linuxPorts = append(linuxPorts[:i], linuxPorts[i+1:]...)
} else {
i++
}
}
}
// get all tcp ports in local windows now
windowsPorts, err := service.GetWindowsHostPorts()
if err != nil {
log.Println(err)
}
// calculate which port need to proxy
needPorts := service.GetNeededProxyPorts(linuxPorts, windowsPorts)
// create proxy
for _, port := range needPorts {
omitted := false
for i, p := range storage.ProxyPool {
if p.Port == port.Port {
omitted = true
// update WslIp and restart proxy (if changed)
if p.WslIp != storage.WslIp {
storage.ProxyPool[i].WslIp = storage.WslIp
_ = p.Stop()
}
if !p.IsRunning {
err := p.Start()
if err != nil {
log.Printf("start proxy error,%s\n", err)
}
}
break
}
}
if !omitted {
newProxy := proxy.Proxy{Port: port.Port, ProxyPort: port.ProxyPort, Type: port.Type, WslIp: storage.WslIp}
err := newProxy.Start()
if err != nil {
log.Printf("start proxy error,%s\n", err)
}
storage.ProxyPool = append(storage.ProxyPool, newProxy)
}
}
// check for delete update
for i := 0; i < len(storage.ProxyPool); {
needToDelete := true
for _, port := range linuxPorts {
if port.Port == storage.ProxyPool[i].Port &&
port.ProxyPort == storage.ProxyPool[i].ProxyPort {
needToDelete = false
break
}
}
if needToDelete {
_ = storage.ProxyPool[i].Stop()
}
// delete
if !storage.ProxyPool[i].IsRunning {
storage.ProxyPool = append(storage.ProxyPool[:i], storage.ProxyPool[i+1:]...)
} else {
i++
}
}
time.Sleep(time.Second * 1)
}
}