-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
157 lines (142 loc) · 4 KB
/
proxy.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
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"
flag "github.com/spf13/pflag"
i2phttpproxy "github.com/eyedeekay/httptunnel"
i2pbrowserproxy "github.com/eyedeekay/httptunnel/multiproxy"
)
var (
watchProfiles = flag.String("watch-profiles", "", "Monitor and control these Firefox profiles. Temporarily Unused.")
aggressiveIsolation = false
samHostString = "127.0.0.1" //flag.String("bridge-host", "127.0.0.1", "host: of the SAM bridge")
samPortString = "7656" //flag.String("bridge-port", "7656", ":port of the SAM bridge")
destfile = "invalid.tunkey"
debugConnection = false
inboundTunnelLength = 3
outboundTunnelLength = 3
inboundTunnels = 4
outboundTunnels = 4
inboundBackups = 2
outboundBackups = 2
inboundVariance = 0
outboundVariance = 0
dontPublishLease = true
reduceIdle = false
useCompression = true
reduceIdleTime = 2000000
reduceIdleQuantity = 1
runQuiet = false
)
func proxy() {
ln, err := net.Listen("tcp", "127.0.0.1:4444")
if err != nil {
log.Fatal(err)
}
cln, err := net.Listen("tcp", "127.0.0.1:7696")
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
go proxyMain(ctx, ln, cln)
<-ctx.Done()
cancel()
}
func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
flag.Parse()
for {
_, err := net.Listen("tcp", samHostString+":"+samPortString)
if err != nil {
break
}
}
profiles := strings.Split(*watchProfiles, ",")
srv := &http.Server{
ReadTimeout: 600 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: ln.Addr().String(),
}
var err error
srv.Handler, err = i2pbrowserproxy.NewHttpProxy(
i2pbrowserproxy.SetHost(samHostString),
i2pbrowserproxy.SetPort(samPortString),
i2pbrowserproxy.SetProxyAddr(ln.Addr().String()),
i2pbrowserproxy.SetControlAddr(cln.Addr().String()),
i2pbrowserproxy.SetDebug(debugConnection),
i2pbrowserproxy.SetInLength(uint(inboundTunnelLength)),
i2pbrowserproxy.SetOutLength(uint(outboundTunnelLength)),
i2pbrowserproxy.SetInQuantity(uint(inboundTunnels)),
i2pbrowserproxy.SetOutQuantity(uint(outboundTunnels)),
i2pbrowserproxy.SetInBackups(uint(inboundBackups)),
i2pbrowserproxy.SetOutBackups(uint(outboundBackups)),
i2pbrowserproxy.SetInVariance(inboundVariance),
i2pbrowserproxy.SetOutVariance(outboundVariance),
i2pbrowserproxy.SetUnpublished(dontPublishLease),
i2pbrowserproxy.SetReduceIdle(reduceIdle),
i2pbrowserproxy.SetCompression(useCompression),
i2pbrowserproxy.SetReduceIdleTime(uint(reduceIdleTime)),
i2pbrowserproxy.SetReduceIdleQuantity(uint(reduceIdleQuantity)),
i2pbrowserproxy.SetKeysPath(destfile),
i2pbrowserproxy.SetProxyMode(aggressiveIsolation),
)
i2pbrowserproxy.Quiet = runQuiet
if err != nil {
log.Fatal(err)
}
ctrlsrv := &http.Server{
ReadHeaderTimeout: 600 * time.Second,
WriteTimeout: 600 * time.Second,
Addr: cln.Addr().String(),
}
ctrlsrv.Handler, err = i2phttpproxy.NewSAMHTTPController(profiles, srv)
if err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
if sig == os.Interrupt {
srv.Handler.(*i2pbrowserproxy.SAMMultiProxy).Close()
srv.Shutdown(ctx)
ctrlsrv.Shutdown(ctx)
}
}
}()
go func() {
log.Println("Starting control server on", cln.Addr())
if err := ctrlsrv.Serve(cln); err != nil {
if err == http.ErrServerClosed {
return
}
log.Fatal("Serve:", err)
}
log.Println("Stopping control server on", cln.Addr())
}()
go func() {
log.Println("Starting proxy server on", ln.Addr())
if err := srv.Serve(ln); err != nil {
if err == http.ErrServerClosed {
return
}
log.Fatal("Serve:", err)
}
log.Println("Stopping proxy server on", ln.Addr())
}()
counter()
<-ctx.Done()
}
func counter() {
var x int
for {
log.Println("Identity is", x, "minute(s) old")
time.Sleep(1 * time.Minute)
x++
}
}