Files
httptunnel/httpproxy/main.go

148 lines
4.7 KiB
Go
Raw Normal View History

package main
import (
"context"
2019-02-21 17:46:33 -05:00
"flag"
"log"
"net"
"net/http"
2019-02-27 22:01:47 -05:00
"os"
"os/signal"
2019-02-23 22:36:17 -05:00
"strings"
2019-02-21 17:46:33 -05:00
"time"
)
import (
2019-03-03 18:10:52 -05:00
. "github.com/eyedeekay/httptunnel"
2019-03-16 23:40:59 -04:00
"crawshaw.io/littleboss"
)
2019-02-21 17:46:33 -05:00
var (
tunnelName = flag.String("service-name", "sam-http-proxy", "Name of the service(can be anything)")
samHostString = flag.String("bridge-host", "127.0.0.1", "host: of the SAM bridge")
samPortString = flag.String("bridge-port", "7656", ":port of the SAM bridge")
watchProfiles = flag.String("watch-profiles", "~/.mozilla/.firefox.profile.i2p.default/user.js,~/.mozilla/.firefox.profile.i2p.debug/user.js", "Monitor and control these Firefox profiles")
2019-03-16 23:40:59 -04:00
destfile = flag.String("dest-file", "invalid.tunkey", "Use a long-term destination key")
2019-02-21 17:46:33 -05:00
debugConnection = flag.Bool("conn-debug", false, "Print connection debug info")
inboundTunnelLength = flag.Int("in-tun-length", 2, "Tunnel Length(default 3)")
outboundTunnelLength = flag.Int("out-tun-length", 2, "Tunnel Length(default 3)")
2019-02-27 22:01:47 -05:00
inboundTunnels = flag.Int("in-tunnels", 2, "Inbound Tunnel Count(default 2)")
outboundTunnels = flag.Int("out-tunnels", 2, "Outbound Tunnel Count(default 2)")
inboundBackups = flag.Int("in-backups", 1, "Inbound Backup Count(default 1)")
outboundBackups = flag.Int("out-backups", 1, "Inbound Backup Count(default 1)")
inboundVariance = flag.Int("in-variance", 0, "Inbound Backup Count(default 0)")
outboundVariance = flag.Int("out-variance", 0, "Inbound Backup Count(default 0)")
2019-02-21 17:46:33 -05:00
dontPublishLease = flag.Bool("no-publish", true, "Don't publish the leaseset(Client mode)")
2019-02-26 22:36:26 -05:00
encryptLease = flag.Bool("encrypt-lease", false, "Encrypt the leaseset(default false, inert)")
reduceIdle = flag.Bool("reduce-idle", false, "Reduce tunnels on extended idle time")
closeIdle = flag.Bool("close-idle", false, "Close tunnels on extended idle time")
closeIdleTime = flag.Int("close-idle-time", 3000000, "Reduce tunnels after time(Ms)")
useCompression = flag.Bool("use-compression", true, "Enable gzip compression")
reduceIdleTime = flag.Int("reduce-idle-time", 2000000, "Reduce tunnels after time(Ms)")
reduceIdleQuantity = flag.Int("reduce-idle-tunnels", 1, "Reduce tunnels to this level")
2019-02-21 17:46:33 -05:00
)
var addr string
func main() {
2019-02-21 17:46:33 -05:00
lb := littleboss.New(*tunnelName)
ln := lb.Listener("proxy-addr", "tcp", "127.0.0.1:7950", "The address of the proxy")
cln := lb.Listener("control-addr", "tcp", "127.0.0.1:7951", "The address of the controller")
lb.Run(func(ctx context.Context) {
2019-02-21 17:46:33 -05:00
proxyMain(ctx, ln.Listener(), cln.Listener())
})
}
2019-02-21 17:46:33 -05:00
func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
flag.Parse()
2019-02-27 22:01:47 -05:00
profiles := strings.Split(*watchProfiles, ",")
2019-02-21 17:46:33 -05:00
srv := &http.Server{
ReadTimeout: 600 * time.Second,
2019-02-27 22:01:47 -05:00
WriteTimeout: 10 * time.Second,
2019-02-26 22:36:26 -05:00
Addr: ln.Addr().String(),
2019-02-21 17:46:33 -05:00
}
var err error
srv.Handler, err = NewHttpProxy(
SetHost(*samHostString),
SetPort(*samPortString),
2019-02-28 13:04:27 -05:00
SetControlAddr(cln.Addr().String()),
2019-02-21 17:46:33 -05:00
SetDebug(*debugConnection),
SetInLength(uint(*inboundTunnelLength)),
SetOutLength(uint(*outboundTunnelLength)),
SetInQuantity(uint(*inboundTunnels)),
SetOutQuantity(uint(*outboundTunnels)),
SetInBackups(uint(*inboundBackups)),
SetOutBackups(uint(*outboundBackups)),
SetInVariance(*inboundVariance),
SetOutVariance(*outboundVariance),
SetUnpublished(*dontPublishLease),
SetReduceIdle(*reduceIdle),
2019-02-26 22:36:26 -05:00
SetCompression(*useCompression),
2019-02-21 17:46:33 -05:00
SetReduceIdleTime(uint(*reduceIdleTime)),
SetReduceIdleQuantity(uint(*reduceIdleQuantity)),
2019-02-26 22:36:26 -05:00
SetCloseIdle(*closeIdle),
SetCloseIdleTime(uint(*closeIdleTime)),
2019-03-16 23:40:59 -04:00
SetKeysPath(*destfile),
2019-02-21 17:46:33 -05:00
)
if err != nil {
log.Fatal(err)
}
2019-02-21 17:46:33 -05:00
ctrlsrv := &http.Server{
2019-02-26 22:36:26 -05:00
ReadHeaderTimeout: 600 * time.Second,
WriteTimeout: 600 * time.Second,
Addr: cln.Addr().String(),
2019-02-21 17:46:33 -05:00
}
ctrlsrv.Handler, err = NewSAMHTTPController(profiles, nil)
2019-02-21 17:46:33 -05:00
2019-02-27 22:01:47 -05:00
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
if sig == os.Interrupt {
srv.Handler.(*SAMHTTPProxy).Close()
srv.Shutdown(ctx)
ctrlsrv.Shutdown(ctx)
}
}
}()
2019-02-21 17:46:33 -05:00
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())
}()
2019-02-20 21:47:11 -05:00
go func() {
log.Println("Starting proxy server on", ln.Addr())
2019-02-21 17:46:33 -05:00
if err := srv.Serve(ln); err != nil {
2019-02-20 21:38:54 -05:00
if err == http.ErrServerClosed {
return
}
log.Fatal("Serve:", err)
}
2019-02-21 17:46:33 -05:00
log.Println("Stopping proxy server on", ln.Addr())
}()
2019-02-21 17:46:33 -05:00
go counter()
<-ctx.Done()
}
2019-02-21 17:46:33 -05:00
func counter() {
var x int
for {
log.Println("Identity is", x, "minute(s) old")
time.Sleep(1 * time.Minute)
x++
}
}