-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyConfig.go
233 lines (197 loc) · 4.8 KB
/
ProxyConfig.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
"regexp"
"strings"
"sync"
)
type Forward struct {
Dst string `json:"dst"`
Src string `json:"src"`
Type string `json:"type"`
Label string `json:"label"`
Silent bool `json:"silent"`
Log string `json:"log"`
Disable bool `json:"disable"`
}
type ProxyConfig struct {
Forward []Forward `json:"forward"`
Silent bool `json:"silent"`
Disable bool `json:"disable"`
filepath string
varspath string
raw string
}
type Vars map[string]string
var ValidForwardTypes = [...]string{"tcp", "udp", "http", "https"}
func (p *ProxyConfig) LoadConfig(filepath string, varspath string) (*ProxyConfig, error) {
p.filepath = filepath
bt, err := os.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
str := string(bt)
if varspath != "" {
p.varspath = varspath
variables := new(Vars)
raw, err := os.ReadFile(varspath)
if err != nil {
log.Fatal("Failed to Open Variables File, Format { \"key1\": \"value1\", \"key2\": \"value2\" ...} ", varspath)
}
err = json.Unmarshal(raw, &variables)
if err != nil {
log.Fatal("Failed to Parse Variables, Format { \"key1\": \"value1\", \"key2\": \"value2\" ...} ", varspath)
}
for k, v := range *variables {
str = strings.ReplaceAll(str, fmt.Sprintf("{{%s}}", k), v)
}
}
if strings.Contains(str, "{{") {
log.Println("Unknown Variables found in Config")
re := regexp.MustCompile(`\{\{([^\}]+)\}\}`)
matches := re.FindAllStringSubmatch(str, -1)
for _, m := range matches {
log.Println(m[0])
}
log.Fatalln("")
}
p.raw = str
err = json.Unmarshal([]byte(str), &p)
if err != nil {
log.Fatal(err)
return nil, err
}
return p, nil
}
func (p *ProxyConfig) Run() {
var wg sync.WaitGroup
if p.Disable {
return
}
if p.Silent {
log.SetOutput(io.Discard)
}
for _, f := range p.Forward {
if f.Disable {
log.Println(f.Label, "Proxy Disabled", "Src: "+f.Src, "Dst: "+f.Dst, "Type:"+f.Type)
continue
}
log.Println(f.Label, "Proxy Running on "+f.Src, "Forwarding to "+f.Dst, "Type:"+f.Type)
wg.Add(1)
go handleForward(f, &wg)
}
wg.Wait()
}
func (f *Forward) UnmarshalJSON(data []byte) error {
type Alias Forward
err := json.Unmarshal(data, (*Alias)(f))
if err != nil {
return err
}
if f.Type == "http" && f.Src == "" {
f.Src = ":80"
}
if f.Type == "https" && f.Src == "" {
f.Src = ":443"
}
if f.Src == f.Dst {
log.Println("Error Parsing Config - src")
log.Fatalln("Invalid Source cannot be same as Destination - ", f.Src, f.Dst, f.Type)
}
foundMatch := false
if f.Type == "" || f.Type == "http" || f.Type == "https" {
f.Type = "tcp"
foundMatch = true
}
if !foundMatch {
for _, k := range ValidForwardTypes {
if k == strings.ToLower(f.Type) {
foundMatch = true
break
}
}
if !foundMatch {
log.Println("Error Parsing Config - type")
log.Fatalln("Invalid Type for Forward Config - ", f.Src, f.Dst, f.Type)
}
}
_, _, err = net.SplitHostPort(f.Src)
if err != nil {
log.Println("Error Parsing Config - src")
log.Fatalln("Invalid Source for Forward Config - ", f.Src, f.Dst, f.Type)
log.Fatalln(err)
return err
}
_, _, err = net.SplitHostPort(f.Dst)
if err != nil {
log.Println("Error Parsing Config - dst")
log.Fatalln("Invalid Destination for Forward Config - ", f.Src, f.Dst, f.Type)
log.Fatalln(err)
return err
}
if f.Label == "" {
f.Label = "Unlabelled"
}
return nil
}
func handleForward(f Forward, wg *sync.WaitGroup) {
ln, err := net.Listen(string(f.Type), f.Src)
if err != nil {
wg.Done()
log.Println(err)
return
}
for {
srcConn, err := ln.Accept()
if err != nil {
wg.Done()
log.Println(err)
return
}
go handleConnection(srcConn, f)
}
}
func handleConnection(srcConn net.Conn, f Forward) {
dstConn, err := net.Dial(string(f.Type), f.Dst)
if err != nil {
if !f.Silent {
log.Printf("Error: Failed: %v", err)
}
defer srcConn.Close()
return
}
go func() {
defer dstConn.Close()
defer srcConn.Close()
if !f.Silent {
log.Println(f.Src, "->", f.Dst)
}
ioCopy(dstConn, srcConn, f)
}()
go func() {
defer dstConn.Close()
defer srcConn.Close()
if !f.Silent {
log.Println(f.Dst, "<-", f.Src)
}
ioCopy(srcConn, dstConn, f)
}()
}
func ioCopy(dst net.Conn, src io.Reader, f Forward) (written int64, err error) {
if f.Log == "" {
return io.Copy(dst, src)
}
fileName := fmt.Sprintf(f.Log)
logfile, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return -1, err
}
defer logfile.Close()
mWriter := io.MultiWriter(logfile, dst)
return io.Copy(mWriter, src)
}