-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (71 loc) · 1.7 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
package main
import (
"context"
"github.com/9seconds/httransform/v2"
"github.com/9seconds/httransform/v2/dialers"
"github.com/9seconds/httransform/v2/executor"
"github.com/9seconds/httransform/v2/layers"
"github.com/cosiner/flag"
"io/ioutil"
"net"
"os"
"os/signal"
"syscall"
"time"
)
type Params struct {
Cert string `names:"--cert, -c" usage:"certificate file" default:"server.crt"`
Key string `names:"--key, -k" usage:"certificate key file" default:"server.key"`
Port string `names:"--port, -p" usage:"proxy port" default:"8080"`
Dns string `names:"--dns, -d" usage:"dns server, if empty the system one will be used"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
for range signals {
cancel()
}
}()
params := &Params{}
err := flag.Commandline.ParseStruct(params)
if err != nil {
panic(err)
}
ca, err := ioutil.ReadFile(params.Cert)
if err != nil {
panic(err)
}
ck, err := ioutil.ReadFile(params.Key)
if err != nil {
panic(err)
}
opts := httransform.ServerOpts{
TLSCertCA: ca,
TLSPrivateKey: ck,
TLSSkipVerify: true,
Layers: []layers.Layer{
layers.ProxyHeadersLayer{},
layers.TimeoutLayer{
Timeout: 3 * time.Minute,
},
},
}
dialer := NewUTLSDialer(dialers.Opts{
TLSSkipVerify: opts.GetTLSSkipVerify(),
}, params.Dns)
opts.Executor = executor.MakeDefaultExecutor(dialer)
proxy, err := httransform.NewServer(ctx, opts)
if err != nil {
panic(err)
}
listener, err := net.Listen("tcp", ":"+params.Port)
if err != nil {
panic(err)
}
if err := proxy.Serve(listener); err != nil {
panic(err)
}
}