-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (135 loc) · 3.96 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"
)
type Settings struct {
ToServer string `json:"ToServer"`
DialupToProxy string `json:"DialupToProxy"`
ListenByServer string `json:"ListenByServer"`
ListenByClient string `json:"ListenByClient"`
BootType string `json:"BootType"`
}
var (
path = flag.String("env", "./example.json", "flags")
settings Settings
dialer = net.Dialer{Timeout: 30 * time.Second}
)
func main() {
flag.Parse()
byteArray, _ := os.ReadFile(*path)
json.Unmarshal(byteArray, &settings)
// 鯖ごとに分岐
switch settings.BootType {
case "Server":
if settings.DialupToProxy == "" {
panic("Failed Get \"DialupToProxy\" Value")
}
if settings.ToServer == "" {
panic("Failed Get \"ToServer\" Value")
}
var err error
// 複数 Session 生成できるように Loop
for {
var proxyConn, serverConn net.Conn
// ProxyとのSesison作成
for {
PrintInfo(fmt.Sprintf("Dial Up Proxy: \"%s\"", settings.DialupToProxy))
proxyConn, err = dialer.Dial("tcp", settings.DialupToProxy)
if !isError("Proxy", err) {
break
}
time.Sleep(5 * time.Second)
}
// ServerとのSesison作成
for {
PrintInfo(fmt.Sprintf("Dial Up Server: \"%s\"", settings.ToServer))
serverConn, err = dialer.Dial("tcp", settings.ToServer)
if !isError("Server", err) {
break
}
time.Sleep(5 * time.Second)
}
// Sessionが使われるまで待機
for {
buf := make([]byte, 8)
n, err := proxyConn.Read(buf)
if string(buf[:n]) == "Next" || err == io.EOF {
break
}
if err != nil {
isError("Proxy failed", err)
}
}
// Proxy Sesison <=> Server Sesison を接続
PrintInfo(fmt.Sprintf("Connect [Proxy]%s <=> [Server]%s", proxyConn.RemoteAddr(), serverConn.RemoteAddr()))
go copyIO(proxyConn, serverConn, false)
go copyIO(serverConn, proxyConn, true)
}
case "Proxy":
if settings.ListenByServer == "" {
panic("Failed Get \"ListenByServer\" Value")
}
if settings.ListenByClient == "" {
panic("Failed Get \"ListenByClient\" Value")
}
// ClientからのSession Trigger 作成
client, err := net.Listen("tcp", settings.ListenByClient)
isError("Client", err)
PrintInfo(fmt.Sprintf("Listen Client Session: \"%s\"", settings.ListenByClient))
// ServerからのSesison Trigger 作成
server, err := net.Listen("tcp", settings.ListenByServer)
isError("Server", err)
PrintInfo(fmt.Sprintf("Listen Server Session: \"%s\"", settings.ListenByServer))
// 複数 Session 生成できるように Loop
for {
// Server との Session を待機
serverConn, err := server.Accept()
isError("Server", err)
PrintInfo(fmt.Sprintf("Catch Server: \"%s\"", serverConn.RemoteAddr()))
// Client との Session を待機
clientConn, err := client.Accept()
isError("Client", err)
PrintInfo(fmt.Sprintf("Catch Client: \"%s\"", clientConn.RemoteAddr()))
// Session を使ったことを通知
_, err = serverConn.Write([]byte("Next"))
isError("Server", err)
time.Sleep(1 * time.Second)
// Client Session <=> Server Session を接続
PrintInfo(fmt.Sprintf("[Server]%s <=> [Client]%s (https://ipinfo.io/%s) ", serverConn.RemoteAddr(), clientConn.RemoteAddr(), strings.Split(clientConn.RemoteAddr().String(), ":")[0]))
// Server <=> Client
go copyIO(serverConn, clientConn, false)
go copyIO(clientConn, serverConn, true)
}
}
}
func copyIO(src, dest net.Conn, shouldClose bool) {
defer func() {
if shouldClose {
time.Sleep(10 * time.Second)
PrintInfo(fmt.Sprintf("Close Session %s <=> %s", src.RemoteAddr(), dest.RemoteAddr()))
err := src.Close()
isError("IOcopy", err)
err = dest.Close()
isError("IOcopy", err)
}
}()
io.Copy(src, dest)
}
func isError(host string, err error) (ok bool) {
if err != nil {
log.Printf("[ERROR]: <%s> %s\n", host, err.Error())
return true
}
return false
}
func PrintInfo(message string) {
log.Printf("[INFO]: %s\n", message)
}