3 Commits

Author SHA1 Message Date
idk
3b144b5294 new version to test for rtrade 2019-10-16 21:11:16 -04:00
idk
db2f32877a check thing 2019-10-16 21:01:56 -04:00
idk
6b993d6d62 move configuration into the struct 2019-09-03 21:38:47 -04:00
10 changed files with 445 additions and 315 deletions

View File

@@ -3,8 +3,15 @@ GO111MODULE=on
GO_COMPILER_OPTS = -a -tags netgo -ldflags '-w -extldflags "-static"'
USER_GH=eyedeekay
packagename=httptunnel
VERSION=0.32.05
httpall: fmt win lin linarm mac
tag:
gothub release -s $(GITHUB_TOKEN) -u $(USER_GH) -r $(packagename) -t v$(VERSION) -d "I2P Tunnel Management tool for Go applications"
include multiproxy/Makefile
opall: fmt opwin oplin oplinarm opmac
@@ -12,6 +19,9 @@ opall: fmt opwin oplin oplinarm opmac
ball:
cd multiproxy && make all
blin64:
cd multiproxy && make blin64
all: httpall opall ball
fmt:

View File

@@ -45,10 +45,9 @@ Features: Done
* CONNECT support
* "New Ident" signaling interface(Unix-only for now)(I guess I might have done
for Windows too now but I haven't tried it out yet).
* Outproxy Support
Features: Planned
-----------------
* Outproxy Support
* Traffic Shaping
* Authentication

6
go.mod
View File

@@ -4,8 +4,10 @@ go 1.12
require (
github.com/eyedeekay/goSam v0.1.1-0.20190814204230-d4c9b8c57dd6
github.com/eyedeekay/sam-forwarder v0.0.0-20190831071254-d67c0c0e311f
github.com/eyedeekay/sam-forwarder v0.0.0-20190908210105-71ca8cd65fda
github.com/eyedeekay/sam3 v0.0.0-20190730185140-f8d54526ea25
github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb // indirect
github.com/mwitkow/go-http-dialer v0.0.0-20161116154839-378f744fb2b8
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
)

View File

@@ -85,21 +85,21 @@ func unixRestart() error {
func (s *SAMHTTPController) windowsStart() error {
var err error
s.ProxyServer.Handler, err = NewHttpProxy(
SetHost(s.ProxyServer.Handler.(*SAMHTTPProxy).SamHost),
SetPort(s.ProxyServer.Handler.(*SAMHTTPProxy).SamPort),
SetHost(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.SamHost),
SetPort(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.SamPort),
SetDebug(s.ProxyServer.Handler.(*SAMHTTPProxy).debug),
SetInLength(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).inLength)),
SetOutLength(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).outLength)),
SetInQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).inQuantity)),
SetOutQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).outQuantity)),
SetInBackups(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).inBackups)),
SetOutBackups(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).outBackups)),
SetInVariance(s.ProxyServer.Handler.(*SAMHTTPProxy).inVariance),
SetOutVariance(s.ProxyServer.Handler.(*SAMHTTPProxy).outVariance),
SetUnpublished(s.ProxyServer.Handler.(*SAMHTTPProxy).dontPublishLease),
SetReduceIdle(s.ProxyServer.Handler.(*SAMHTTPProxy).reduceIdle),
SetReduceIdleTime(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).reduceIdleTime)),
SetReduceIdleQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).reduceIdleQuantity)),
SetInLength(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.InLength)),
SetOutLength(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.OutLength)),
SetInQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.InQuantity)),
SetOutQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.OutQuantity)),
SetInBackups(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.InBackupQuantity)),
SetOutBackups(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.OutBackupQuantity)),
SetInVariance(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.InVariance),
SetOutVariance(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.OutVariance),
SetUnpublished(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.Client),
SetReduceIdle(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.ReduceIdle),
SetReduceIdleTime(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.ReduceIdleTime)),
SetReduceIdleQuantity(uint(s.ProxyServer.Handler.(*SAMHTTPProxy).Conf.ReduceIdleQuantity)),
)
if err != nil {
log.Fatal(err)

View File

@@ -24,7 +24,7 @@ var (
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")
destfile = flag.String("dest-file", "invalid.tunkey", "Use a long-term destination key")
debugConnection = flag.Bool("conn-debug", true, "Print connection debug info")
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)")
inboundTunnels = flag.Int("in-tunnels", 2, "Inbound Tunnel Count(default 2)")
@@ -43,8 +43,10 @@ var (
reduceIdleQuantity = flag.Int("reduce-idle-tunnels", 1, "Reduce tunnels to this level")
runCommand = flag.String("run-command", "", "Execute command using the *_PROXY environment variables")
runArguments = flag.String("run-arguments", "", "Pass arguments to run-command")
suppressLifetime = flag.Bool("suppress-lifetime-output", false, "Suppress \"Tunnel lifetime\" output")
suppressLifetime = flag.Bool("suppress-lifetime-output", true, "Suppress \"Tunnel lifetime\" output")
runQuiet = flag.Bool("run-quiet", false, "Suppress all non-command output")
outproxy = flag.String("outproxy-addr", "false.i2p", "Use this address as an outproxy, either a base32 address or a local HTTP proxy")
socks = flag.Bool("outproxy-socks", true, "Use a SOCKS outproxy")
)
var addr string
@@ -86,6 +88,8 @@ func proxyMain(ctx context.Context, ln net.Listener, cln net.Listener) {
SetPort(*samPortString),
SetProxyAddr(ln.Addr().String()),
SetControlAddr(cln.Addr().String()),
SetOutProxy(*outproxy),
SetOutProxySocks(*socks),
SetDebug(*debugConnection),
SetInLength(uint(*inboundTunnelLength)),
SetOutLength(uint(*outboundTunnelLength)),

View File

@@ -13,7 +13,7 @@ type Option func(*SAMHTTPProxy) error
//SetName sets a clients's address in the form host:port or host, port
func SetName(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.tunName = s
c.Conf.TunName = s
return nil
}
}
@@ -26,8 +26,8 @@ func SetAddr(s ...string) func(*SAMHTTPProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.SamHost = split[0]
c.SamPort = split[1]
c.Conf.SamHost = split[0]
c.Conf.SamPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -38,8 +38,8 @@ func SetAddr(s ...string) func(*SAMHTTPProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.SamHost = s[0]
c.SamPort = s[1]
c.Conf.SamHost = s[0]
c.Conf.SamPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -59,8 +59,8 @@ func SetControlAddr(s ...string) func(*SAMHTTPProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.controlHost = split[0]
c.controlPort = split[1]
c.Conf.ControlHost = split[0]
c.Conf.ControlPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -71,8 +71,8 @@ func SetControlAddr(s ...string) func(*SAMHTTPProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.controlHost = s[0]
c.controlPort = s[1]
c.Conf.ControlHost = s[0]
c.Conf.ControlPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -92,8 +92,8 @@ func SetProxyAddr(s ...string) func(*SAMHTTPProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.proxyHost = split[0]
c.proxyPort = split[1]
c.Conf.TargetHost = split[0]
c.Conf.TargetPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -104,8 +104,8 @@ func SetProxyAddr(s ...string) func(*SAMHTTPProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.proxyHost = s[0]
c.proxyPort = s[1]
c.Conf.TargetHost = s[0]
c.Conf.TargetPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -121,8 +121,8 @@ func SetProxyAddr(s ...string) func(*SAMHTTPProxy) error {
func SetAddrMixed(s string, i int) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if i < 65536 && i > 0 {
c.SamHost = s
c.SamPort = strconv.Itoa(i)
c.Conf.SamHost = s
c.Conf.SamPort = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
@@ -132,7 +132,7 @@ func SetAddrMixed(s string, i int) func(*SAMHTTPProxy) error {
//SetContrlHost sets the host of the client's Proxy controller
func SetControlHost(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.controlHost = s
c.Conf.ControlHost = s
return nil
}
}
@@ -145,7 +145,7 @@ func SetControlPort(s string) func(*SAMHTTPProxy) error {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.controlPort = s
c.Conf.ControlPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -155,7 +155,27 @@ func SetControlPort(s string) func(*SAMHTTPProxy) error {
//SetProxyHost sets the host of the client's Proxy controller
func SetProxyHost(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.proxyHost = s
c.Conf.TargetHost = s
return nil
}
}
//SetOutProxy sets the host of the client's outproxy, it may be a base32 or a local proxy
func SetOutProxy(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.UseOutProxy = s
return nil
}
}
//SetOutProxySocks tells it to use a SOCKS outproxy instead of HTTP
func SetOutProxySocks(s bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if s {
c.outproxytype = "socks://"
return nil
}
c.outproxytype = "http://"
return nil
}
}
@@ -168,7 +188,7 @@ func SetProxyPort(s string) func(*SAMHTTPProxy) error {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.proxyPort = s
c.Conf.TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -178,7 +198,7 @@ func SetProxyPort(s string) func(*SAMHTTPProxy) error {
//SetKeysPath sets the path to the key save files
func SetKeysPath(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.keyspath = s
c.Conf.KeyFilePath = s
return nil
}
}
@@ -186,7 +206,7 @@ func SetKeysPath(s string) func(*SAMHTTPProxy) error {
//SetHost sets the host of the client's SAM bridge
func SetHost(s string) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -199,7 +219,7 @@ func SetPort(s string) func(*SAMHTTPProxy) error {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -210,7 +230,7 @@ func SetPort(s string) func(*SAMHTTPProxy) error {
func SetPortInt(i int) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if i < 65536 && i > -1 {
c.SamPort = strconv.Itoa(i)
c.Conf.SamPort = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
@@ -229,7 +249,7 @@ func SetDebug(b bool) func(*SAMHTTPProxy) error {
func SetInLength(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u < 7 {
c.inLength = u
c.Conf.InLength = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -240,7 +260,7 @@ func SetInLength(u uint) func(*SAMHTTPProxy) error {
func SetOutLength(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u < 7 {
c.outLength = u
c.Conf.OutLength = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -251,7 +271,7 @@ func SetOutLength(u uint) func(*SAMHTTPProxy) error {
func SetInVariance(i int) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if i < 7 && i > -7 {
c.inVariance = i
c.Conf.InVariance = int(i)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -262,7 +282,7 @@ func SetInVariance(i int) func(*SAMHTTPProxy) error {
func SetOutVariance(i int) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if i < 7 && i > -7 {
c.outVariance = i
c.Conf.OutVariance = int(i)
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -273,7 +293,7 @@ func SetOutVariance(i int) func(*SAMHTTPProxy) error {
func SetInQuantity(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u <= 16 {
c.inQuantity = u
c.Conf.InQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -284,7 +304,7 @@ func SetInQuantity(u uint) func(*SAMHTTPProxy) error {
func SetOutQuantity(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u <= 16 {
c.outQuantity = u
c.Conf.OutQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -295,7 +315,7 @@ func SetOutQuantity(u uint) func(*SAMHTTPProxy) error {
func SetInBackups(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u < 6 {
c.inBackups = u
c.Conf.InBackupQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -306,7 +326,7 @@ func SetInBackups(u uint) func(*SAMHTTPProxy) error {
func SetOutBackups(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u < 6 {
c.outBackups = u
c.Conf.OutBackupQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -316,7 +336,7 @@ func SetOutBackups(u uint) func(*SAMHTTPProxy) error {
//SetUnpublished tells the router to not publish the client leaseset
func SetUnpublished(b bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.dontPublishLease = b
c.Conf.Client = b
return nil
}
}
@@ -324,7 +344,7 @@ func SetUnpublished(b bool) func(*SAMHTTPProxy) error {
//SetEncrypt tells the router to use an encrypted leaseset
func SetEncrypt(b bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.encryptLease = b
c.Conf.EncryptLeaseSet = b
return nil
}
}
@@ -332,7 +352,7 @@ func SetEncrypt(b bool) func(*SAMHTTPProxy) error {
//SetReduceIdle sets the created tunnels to be reduced during extended idle time to avoid excessive resource usage
func SetReduceIdle(b bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.reduceIdle = b
c.Conf.ReduceIdle = b
return nil
}
}
@@ -341,7 +361,7 @@ func SetReduceIdle(b bool) func(*SAMHTTPProxy) error {
func SetReduceIdleTime(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u > 299999 {
c.reduceIdleTime = u
c.Conf.ReduceIdleTime = int(u)
return nil
}
return fmt.Errorf("Invalid reduce idle time %v", u)
@@ -352,7 +372,7 @@ func SetReduceIdleTime(u uint) func(*SAMHTTPProxy) error {
func SetReduceIdleQuantity(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
if u < 5 {
c.reduceIdleQuantity = u
c.Conf.ReduceIdleQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid reduced tunnel quantity %v", u)
@@ -362,7 +382,7 @@ func SetReduceIdleQuantity(u uint) func(*SAMHTTPProxy) error {
//SetCompression sets the tunnels to close after a specific amount of time
func SetCompression(b bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.compression = b
c.Conf.UseCompression = b
return nil
}
}
@@ -370,7 +390,7 @@ func SetCompression(b bool) func(*SAMHTTPProxy) error {
//SetCloseIdle enables debugging messages
func SetCloseIdle(b bool) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
c.closeIdle = b
c.Conf.CloseIdle = b
return nil
}
}
@@ -380,7 +400,7 @@ func SetCloseIdleTime(u uint) func(*SAMHTTPProxy) error {
return func(c *SAMHTTPProxy) error {
log.Println("TEST CLOSE", u, (u > 299999))
if u > 299999 {
c.closeIdleTime = u
c.Conf.CloseIdleTime = int(u)
return nil
}
return fmt.Errorf("Invalid close idle time %v", u)
@@ -389,87 +409,87 @@ func SetCloseIdleTime(u uint) func(*SAMHTTPProxy) error {
//return the inbound length as a string.
func (c *SAMHTTPProxy) inlength() string {
return fmt.Sprintf("inbound.length=%d", c.inLength)
return fmt.Sprintf("inbound.length=%d", c.Conf.InLength)
}
//return the outbound length as a string.
func (c *SAMHTTPProxy) outlength() string {
return fmt.Sprintf("outbound.length=%d", c.outLength)
return fmt.Sprintf("outbound.length=%d", c.Conf.OutLength)
}
//return the inbound length variance as a string.
func (c *SAMHTTPProxy) invariance() string {
return fmt.Sprintf("inbound.lengthVariance=%d", c.inVariance)
return fmt.Sprintf("inbound.lengthVariance=%d", c.Conf.InVariance)
}
//return the outbound length variance as a string.
func (c *SAMHTTPProxy) outvariance() string {
return fmt.Sprintf("outbound.lengthVariance=%d", c.outVariance)
return fmt.Sprintf("outbound.lengthVariance=%d", c.Conf.OutVariance)
}
//return the inbound tunnel quantity as a string.
func (c *SAMHTTPProxy) inquantity() string {
return fmt.Sprintf("inbound.quantity=%d", c.inQuantity)
return fmt.Sprintf("inbound.quantity=%d", c.Conf.InQuantity)
}
//return the outbound tunnel quantity as a string.
func (c *SAMHTTPProxy) outquantity() string {
return fmt.Sprintf("outbound.quantity=%d", c.outQuantity)
return fmt.Sprintf("outbound.quantity=%d", c.Conf.OutQuantity)
}
//return the inbound tunnel quantity as a string.
func (c *SAMHTTPProxy) inbackups() string {
return fmt.Sprintf("inbound.backupQuantity=%d", c.inQuantity)
return fmt.Sprintf("inbound.backupQuantity=%d", c.Conf.InQuantity)
}
//return the outbound tunnel quantity as a string.
func (c *SAMHTTPProxy) outbackups() string {
return fmt.Sprintf("outbound.backupQuantity=%d", c.outQuantity)
return fmt.Sprintf("outbound.backupQuantity=%d", c.Conf.OutQuantity)
}
func (c *SAMHTTPProxy) encryptlease() string {
if c.encryptLease {
if c.Conf.EncryptLeaseSet {
return "i2cp.encryptLeaseSet=true"
}
return "i2cp.encryptLeaseSet=false"
}
func (c *SAMHTTPProxy) dontpublishlease() string {
if c.dontPublishLease {
if c.Conf.Client {
return "i2cp.dontPublishLeaseSet=true"
}
return "i2cp.dontPublishLeaseSet=false"
}
func (c *SAMHTTPProxy) reduceonidle() string {
if c.reduceIdle {
if c.Conf.ReduceIdle {
return "i2cp.reduceOnIdle=true"
}
return "i2cp.reduceOnIdle=false"
}
func (c *SAMHTTPProxy) reduceidletime() string {
return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.reduceIdleTime)
return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.Conf.ReduceIdleTime)
}
func (c *SAMHTTPProxy) reduceidlecount() string {
return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.reduceIdleQuantity)
return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.Conf.ReduceIdleQuantity)
}
func (c *SAMHTTPProxy) usecompresion() string {
if c.compression {
if c.Conf.UseCompression {
return "i2cp.gzip=true"
}
return "i2cp.gzip=false"
}
func (c *SAMHTTPProxy) closeonidle() string {
if c.reduceIdle {
if c.Conf.ReduceIdle {
return "i2cp.closeOnIdle=true"
}
return "i2cp.closeOnIdle=false"
}
func (c *SAMHTTPProxy) closeidletime() string {
return fmt.Sprintf("i2cp.closeIdleTime=%d", c.reduceIdleTime)
return fmt.Sprintf("i2cp.closeIdleTime=%d", c.Conf.ReduceIdleTime)
}

View File

@@ -3,12 +3,14 @@ package i2phttpproxy
import (
"crypto/tls"
"fmt"
"golang.org/x/net/proxy"
"golang.org/x/time/rate"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
@@ -23,43 +25,24 @@ import (
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam-forwarder/interface"
"github.com/eyedeekay/sam-forwarder/tcp"
"github.com/eyedeekay/sam3/i2pkeys"
"github.com/mwitkow/go-http-dialer"
"github.com/phayes/freeport"
)
type SAMHTTPProxy struct {
goSam *goSam.Client
Hasher *hashhash.Hasher
client *http.Client
transport *http.Transport
rateLimiter *rate.Limiter
tunName string
sigType string
proxyHost string
proxyPort string
SamHost string
SamPort string
controlHost string
controlPort string
destination string
keyspath string
inLength uint
outLength uint
inVariance int
outVariance int
inQuantity uint
outQuantity uint
inBackups uint
outBackups uint
dontPublishLease bool
encryptLease bool
reduceIdle bool
reduceIdleTime uint
reduceIdleQuantity uint
closeIdle bool
closeIdleTime uint
compression bool
goSam *goSam.Client
Hasher *hashhash.Hasher
client *http.Client
outproxyclient *http.Client
transport *http.Transport
rateLimiter *rate.Limiter
outproxy *samforwarder.SAMClientForwarder
outproxydialer proxy.Dialer
outproxytype string
useOutProxy bool
UseOutProxy string
dialed bool
debug bool
@@ -89,7 +72,7 @@ func (f *SAMHTTPProxy) GetType() string {
}
func (f *SAMHTTPProxy) ID() string {
return f.tunName
return f.Conf.TunName
}
func (f *SAMHTTPProxy) Keys() i2pkeys.I2PKeys {
@@ -132,7 +115,7 @@ func (p *SAMHTTPProxy) Search(search string) string {
}
func (p *SAMHTTPProxy) Target() string {
return p.proxyHost + ":" + p.proxyPort
return p.Conf.TargetHost + ":" + p.Conf.TargetPort
}
func (p *SAMHTTPProxy) Base32() string {
@@ -153,7 +136,7 @@ func (p *SAMHTTPProxy) Base64() string {
}
func (p *SAMHTTPProxy) Serve() error {
ln, err := net.Listen("tcp", p.proxyHost+":"+p.proxyPort)
ln, err := net.Listen("tcp", p.Conf.TargetHost+":"+p.Conf.TargetPort)
if err != nil {
return err
}
@@ -210,26 +193,27 @@ func (p *SAMHTTPProxy) freshSAMClient() (*goSam.Client, error) {
//return the combined host:port of the SAM bridge
func (p *SAMHTTPProxy) samaddr() string {
return fmt.Sprintf("%s:%s", p.SamHost, p.SamPort)
return fmt.Sprintf("%s:%s", p.Conf.SamHost, p.Conf.SamPort)
}
func (p *SAMHTTPProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
plog(req.RemoteAddr, " ", req.Method, " ", req.URL)
p.Save()
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
/*if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
if !(req.Method == http.MethodConnect) {
msg := "Unsupported protocol scheme " + req.URL.Scheme
http.Error(wr, msg, http.StatusBadRequest)
plog(msg)
return
}
}*/
if req.URL.Host == p.Conf.ControlHost+":"+p.Conf.ControlPort {
p.reset(wr, req)
return
}
if !strings.HasSuffix(req.URL.Host, ".i2p") {
if req.URL.Host == p.controlHost+":"+p.controlPort {
p.reset(wr, req)
return
}
if !strings.HasSuffix(req.URL.Host, ".i2p") && p.UseOutProxy == "" {
msg := "Unsupported host " + req.URL.Host
if !Quiet {
http.Error(wr, msg, http.StatusBadRequest)
@@ -242,6 +226,13 @@ func (p *SAMHTTPProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
p.get(wr, req)
return
} else {
if !strings.HasSuffix(req.URL.Host, ".i2p") && p.UseOutProxy != "" {
p.outproxyget(wr, req)
return
} else {
plog("No outproxy configured ", p.UseOutProxy, p.outproxy.Target())
return
}
p.connect(wr, req)
return
}
@@ -277,10 +268,10 @@ func UnProxyLocal(additionalAddresses []string) {
}
func (p *SAMHTTPProxy) reset(wr http.ResponseWriter, req *http.Request) {
plog("Validating control access from", req.RemoteAddr, p.controlHost+":"+p.controlPort)
if strings.SplitN(req.RemoteAddr, ":", 2)[0] == p.controlHost {
plog("Validated control access from", req.RemoteAddr, p.controlHost+":"+p.controlPort)
resp, err := http.Get("http://" + p.controlHost + ":" + p.controlPort)
plog("Validating control access from", req.RemoteAddr, p.Conf.ControlHost+":"+p.Conf.ControlPort)
if strings.SplitN(req.RemoteAddr, ":", 2)[0] == p.Conf.ControlHost {
plog("Validated control access from", req.RemoteAddr, p.Conf.ControlHost+":"+p.Conf.ControlPort)
resp, err := http.Get("http://" + p.Conf.ControlHost + ":" + p.Conf.ControlPort)
if err == nil {
wr.Header().Set("Content-Type", "text/html; charset=utf-8")
wr.Header().Set("Access-Control-Allow-Origin", "*")
@@ -291,10 +282,35 @@ func (p *SAMHTTPProxy) reset(wr http.ResponseWriter, req *http.Request) {
}
}
func (p *SAMHTTPProxy) outproxyget(wr http.ResponseWriter, req *http.Request) {
plog("CONNECT via outproxy to", req.URL.Host)
dest_conn, err := p.outproxydialer.Dial("tcp", req.URL.String())
if err != nil {
if !Quiet {
plog(err.Error())
}
return
}
hijacker, ok := wr.(http.Hijacker)
if !ok {
return
}
client_conn, _, err := hijacker.Hijack()
if err != nil {
if !Quiet {
plog(err.Error())
}
return
}
go proxycommon.Transfer(dest_conn, client_conn)
go proxycommon.Transfer(client_conn, dest_conn)
}
func (p *SAMHTTPProxy) get(wr http.ResponseWriter, req *http.Request) {
req.RequestURI = ""
proxycommon.DelHopHeaders(req.Header)
p.client = p.freshClient()
plog("Getting i2p page")
//p.client = p.freshClient()
resp, err := p.client.Do(req)
if err != nil {
msg := "Proxy Error " + err.Error()
@@ -340,18 +356,18 @@ func (f *SAMHTTPProxy) Up() bool {
}
func (p *SAMHTTPProxy) Save() string {
if p.keyspath != "invalid.tunkey" {
if _, err := os.Stat(p.keyspath); os.IsNotExist(err) {
if p.Conf.KeyFilePath != "invalid.tunkey" {
if _, err := os.Stat(p.Conf.KeyFilePath); os.IsNotExist(err) {
if p.goSam != nil {
if p.goSam.Destination() != "" {
ioutil.WriteFile(p.keyspath, []byte(p.goSam.Destination()), 0644)
p.destination = p.goSam.Destination()
ioutil.WriteFile(p.Conf.KeyFilePath, []byte(p.goSam.Destination()), 0644)
p.Conf.ClientDest = p.goSam.Destination()
return p.goSam.Destination()
}
}
} else {
if keys, err := ioutil.ReadFile(p.keyspath); err == nil {
p.destination = string(keys)
if keys, err := ioutil.ReadFile(p.Conf.KeyFilePath); err == nil {
p.Conf.ClientDest = string(keys)
return string(keys)
}
}
@@ -359,33 +375,162 @@ func (p *SAMHTTPProxy) Save() string {
return ""
}
func (p *SAMHTTPProxy) GuaranteePrefix(str string) string {
if strings.HasPrefix(p.outproxytype, str) {
return str
}
return p.outproxytype + str
}
func (handler *SAMHTTPProxy) Load() (samtunnel.SAMTunnel, error) {
var err error
handler.destination = handler.Save()
handler.Conf.ClientDest = handler.Save()
handler.goSam, err = goSam.NewClientFromOptions(
goSam.SetHost(handler.SamHost),
goSam.SetPort(handler.SamPort),
goSam.SetUnpublished(handler.dontPublishLease),
goSam.SetInLength(handler.inLength),
goSam.SetOutLength(handler.outLength),
goSam.SetInQuantity(handler.inQuantity),
goSam.SetOutQuantity(handler.outQuantity),
goSam.SetInBackups(handler.inBackups),
goSam.SetOutBackups(handler.outBackups),
goSam.SetReduceIdle(handler.reduceIdle),
goSam.SetReduceIdleTime(handler.reduceIdleTime),
goSam.SetReduceIdleQuantity(handler.reduceIdleQuantity),
goSam.SetCloseIdle(handler.closeIdle),
goSam.SetCloseIdleTime(handler.closeIdleTime),
goSam.SetCompression(handler.compression),
goSam.SetHost(handler.Conf.SamHost),
goSam.SetPort(handler.Conf.SamPort),
goSam.SetUnpublished(handler.Conf.Client),
goSam.SetInLength(uint(handler.Conf.InLength)),
goSam.SetOutLength(uint(handler.Conf.OutLength)),
goSam.SetInQuantity(uint(handler.Conf.InQuantity)),
goSam.SetOutQuantity(uint(handler.Conf.OutQuantity)),
goSam.SetInBackups(uint(handler.Conf.InBackupQuantity)),
goSam.SetOutBackups(uint(handler.Conf.OutBackupQuantity)),
goSam.SetReduceIdle(handler.Conf.ReduceIdle),
goSam.SetReduceIdleTime(uint(handler.Conf.ReduceIdleTime)),
goSam.SetReduceIdleQuantity(uint(handler.Conf.ReduceIdleQuantity)),
goSam.SetCloseIdle(handler.Conf.CloseIdle),
goSam.SetCloseIdleTime(uint(handler.Conf.CloseIdleTime)),
goSam.SetCompression(handler.Conf.UseCompression),
goSam.SetDebug(handler.debug),
goSam.SetLocalDestination(handler.destination),
goSam.SetLocalDestination(handler.Conf.ClientDest),
)
if err != nil {
return nil, err
}
handler.transport = handler.freshTransport()
handler.client = handler.freshClient()
if handler.UseOutProxy != "" {
if strings.HasSuffix(handler.UseOutProxy, ".i2p") {
plog("Configuring an outproxy,", handler.UseOutProxy)
config := handler.Conf
port, err := freeport.GetFreePort()
if err != nil {
return nil, err
}
config.TargetPort = strconv.Itoa(port)
config.TunName = handler.Conf.TunName + "-outproxy"
config.ClientDest = handler.UseOutProxy
handler.outproxy, err = samforwarder.NewSAMClientForwarderFromOptions(
samforwarder.SetClientSaveFile(config.SaveFile),
samforwarder.SetClientFilePath(config.SaveDirectory),
samforwarder.SetClientHost(config.TargetHost),
samforwarder.SetClientPort(config.TargetPort),
samforwarder.SetClientSAMHost(config.SamHost),
samforwarder.SetClientSAMPort(config.SamPort),
samforwarder.SetClientSigType(config.SigType),
samforwarder.SetClientName(config.TunName),
samforwarder.SetClientInLength(config.InLength),
samforwarder.SetClientOutLength(config.OutLength),
samforwarder.SetClientInVariance(config.InVariance),
samforwarder.SetClientOutVariance(config.OutVariance),
samforwarder.SetClientInQuantity(config.InQuantity),
samforwarder.SetClientOutQuantity(config.OutQuantity),
samforwarder.SetClientInBackups(config.InBackupQuantity),
samforwarder.SetClientOutBackups(config.OutBackupQuantity),
samforwarder.SetClientEncrypt(config.EncryptLeaseSet),
samforwarder.SetClientLeaseSetKey(config.LeaseSetKey),
samforwarder.SetClientLeaseSetPrivateKey(config.LeaseSetPrivateKey),
samforwarder.SetClientLeaseSetPrivateSigningKey(config.LeaseSetPrivateSigningKey),
samforwarder.SetClientAllowZeroIn(config.InAllowZeroHop),
samforwarder.SetClientAllowZeroOut(config.OutAllowZeroHop),
samforwarder.SetClientFastRecieve(config.FastRecieve),
samforwarder.SetClientCompress(config.UseCompression),
samforwarder.SetClientReduceIdle(config.ReduceIdle),
samforwarder.SetClientReduceIdleTimeMs(config.ReduceIdleTime),
samforwarder.SetClientReduceIdleQuantity(config.ReduceIdleQuantity),
samforwarder.SetClientCloseIdle(config.CloseIdle),
samforwarder.SetClientCloseIdleTimeMs(config.CloseIdleTime),
samforwarder.SetClientAccessListType(config.AccessListType),
samforwarder.SetClientAccessList(config.AccessList),
samforwarder.SetClientMessageReliability(config.MessageReliability),
samforwarder.SetClientPassword(config.KeyFilePath),
samforwarder.SetClientDestination(config.ClientDest),
)
if err != nil {
return nil, err
}
go handler.outproxy.Serve()
if handler.outproxytype == "http://" {
proxyURL, err := url.Parse(handler.GuaranteePrefix(handler.outproxy.Target()))
if err != nil {
return nil, err
}
handler.outproxydialer = http_dialer.New(proxyURL, http_dialer.WithTls(&tls.Config{}))
if err != nil {
return nil, err
}
handler.outproxyclient = &http.Client{
Transport: &http.Transport{
Dial: handler.outproxydialer.Dial,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{},
},
Timeout: time.Second * 300,
CheckRedirect: nil,
}
} else {
handler.outproxydialer, err = proxy.SOCKS5("tcp", handler.outproxy.Target(), nil, nil)
if err != nil {
return nil, err
}
handler.outproxyclient = &http.Client{
Transport: &http.Transport{
Dial: handler.outproxydialer.Dial,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{},
},
Timeout: time.Second * 300,
CheckRedirect: nil,
}
}
plog("setup outproxy on", handler.outproxy.Target())
} else {
if handler.outproxytype == "http://" {
proxyURL, err := url.Parse(handler.GuaranteePrefix(handler.UseOutProxy))
if err != nil {
return nil, err
}
handler.outproxydialer = http_dialer.New(proxyURL, http_dialer.WithTls(&tls.Config{}))
if err != nil {
return nil, err
}
handler.outproxyclient = &http.Client{
Transport: &http.Transport{
Dial: handler.outproxydialer.Dial,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{},
},
Timeout: time.Second * 300,
CheckRedirect: nil,
}
} else {
handler.outproxydialer, err = proxy.SOCKS5("tcp", handler.UseOutProxy, nil, proxy.Direct)
if err != nil {
return nil, err
}
handler.outproxyclient = &http.Client{
Transport: &http.Transport{
Dial: handler.outproxydialer.Dial,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{},
},
Timeout: time.Second * 300,
CheckRedirect: nil,
}
}
plog("setup outproxy on", handler.GuaranteePrefix(handler.UseOutProxy))
}
}
handler.Hasher, err = hashhash.NewHasher(len(strings.Replace(handler.Base32(), ".b32.i2p", "", 1)))
if err != nil {
return nil, err
@@ -396,31 +541,13 @@ func (handler *SAMHTTPProxy) Load() (samtunnel.SAMTunnel, error) {
func NewHttpProxy(opts ...func(*SAMHTTPProxy) error) (*SAMHTTPProxy, error) {
var handler SAMHTTPProxy
handler.SamHost = "127.0.0.1"
handler.SamPort = "7656"
handler.controlHost = "127.0.0.1"
handler.controlPort = "7951"
handler.proxyHost = "127.0.0.1"
handler.proxyPort = "7950"
handler.inLength = 2
handler.outLength = 2
handler.inVariance = 0
handler.outVariance = 0
handler.inQuantity = 1
handler.outQuantity = 1
handler.inBackups = 1
handler.outBackups = 1
handler.dontPublishLease = true
handler.encryptLease = false
handler.reduceIdle = false
handler.reduceIdleTime = 2000000
handler.closeIdleTime = 3000000
handler.reduceIdleQuantity = 1
handler.useOutProxy = false
handler.compression = true
handler.tunName = "0"
handler.keyspath = "invalid.tunkey"
handler.destination = ""
handler.Conf = &i2ptunconf.Conf{}
handler.Conf.SamHost = "127.0.0.1"
handler.Conf.SamPort = "7656"
handler.Conf.ControlHost = "127.0.0.1"
handler.Conf.ControlPort = "7951"
handler.UseOutProxy = ""
handler.outproxytype = "http://"
for _, o := range opts {
if err := o(&handler); err != nil {
return nil, err

View File

@@ -28,3 +28,16 @@ It also has an aggressive mode, which creates a whole new tunnel pool for every
single eepSite you visit, by domain(which means that if you visit both the
base32 and readable domain, it will create *two* destinations). I advise against
using it.
Features: Done
--------------
* Self-supervising, Self-restarting on Unixes
* CONNECT support
* "New Ident" signaling interface
Features: Planned
-----------------
* Outproxy Support
* Traffic Shaping

View File

@@ -13,7 +13,7 @@ type Option func(*SAMMultiProxy) error
//SetName sets a clients's address in the form host:port or host, port
func SetName(s string) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.tunName = s
c.Conf.TunName = s
return nil
}
}
@@ -26,8 +26,8 @@ func SetAddr(s ...string) func(*SAMMultiProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.SamHost = split[0]
c.SamPort = split[1]
c.Conf.SamHost = split[0]
c.Conf.SamPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -38,8 +38,8 @@ func SetAddr(s ...string) func(*SAMMultiProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.SamHost = s[0]
c.SamPort = s[1]
c.Conf.SamHost = s[0]
c.Conf.SamPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -59,8 +59,8 @@ func SetControlAddr(s ...string) func(*SAMMultiProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.controlHost = split[0]
c.controlPort = split[1]
c.Conf.ControlHost = split[0]
c.Conf.ControlPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -71,8 +71,8 @@ func SetControlAddr(s ...string) func(*SAMMultiProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.controlHost = s[0]
c.controlPort = s[1]
c.Conf.ControlHost = s[0]
c.Conf.ControlPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -92,8 +92,8 @@ func SetProxyAddr(s ...string) func(*SAMMultiProxy) error {
if len(split) == 2 {
if i, err := strconv.Atoi(split[1]); err == nil {
if i < 65536 {
c.proxyHost = split[0]
c.proxyPort = split[1]
c.Conf.TargetHost = split[0]
c.Conf.TargetPort = split[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -104,8 +104,8 @@ func SetProxyAddr(s ...string) func(*SAMMultiProxy) error {
} else if len(s) == 2 {
if i, err := strconv.Atoi(s[1]); err == nil {
if i < 65536 {
c.proxyHost = s[0]
c.proxyPort = s[1]
c.Conf.TargetHost = s[0]
c.Conf.TargetPort = s[1]
return nil
}
return fmt.Errorf("Invalid port")
@@ -121,18 +121,18 @@ func SetProxyAddr(s ...string) func(*SAMMultiProxy) error {
func SetAddrMixed(s string, i int) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if i < 65536 && i > 0 {
c.SamHost = s
c.SamPort = strconv.Itoa(i)
c.Conf.SamHost = s
c.Conf.SamPort = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
}
}
//SetContrlHost sets the host of the client's Proxy controller
//SetContrlHost sets the host of the client's Proxy Controller
func SetControlHost(s string) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.controlHost = s
c.Conf.ControlHost = s
return nil
}
}
@@ -140,12 +140,12 @@ func SetControlHost(s string) func(*SAMMultiProxy) error {
//SetKeysPath sets the path to the key save files
func SetKeysPath(s string) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.keyspath = s
c.Conf.KeyFilePath = s
return nil
}
}
//SetContrlPort sets the host of the client's Proxy controller
//SetContrlPort sets the host of the client's Proxy Controller
func SetControlPort(s string) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
port, err := strconv.Atoi(s)
@@ -153,7 +153,7 @@ func SetControlPort(s string) func(*SAMMultiProxy) error {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.controlPort = s
c.Conf.ControlPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -163,7 +163,7 @@ func SetControlPort(s string) func(*SAMMultiProxy) error {
//SetHost sets the host of the client's SAM bridge
func SetHost(s string) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -176,7 +176,7 @@ func SetPort(s string) func(*SAMMultiProxy) error {
return fmt.Errorf("Invalid port; non-number")
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -187,7 +187,7 @@ func SetPort(s string) func(*SAMMultiProxy) error {
func SetPortInt(i int) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if i < 65536 && i > -1 {
c.SamPort = strconv.Itoa(i)
c.Conf.SamPort = strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid port")
@@ -206,7 +206,7 @@ func SetDebug(b bool) func(*SAMMultiProxy) error {
func SetInLength(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u < 7 {
c.inLength = u
c.Conf.InLength = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -217,7 +217,7 @@ func SetInLength(u uint) func(*SAMMultiProxy) error {
func SetOutLength(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u < 7 {
c.outLength = u
c.Conf.OutLength = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -228,7 +228,7 @@ func SetOutLength(u uint) func(*SAMMultiProxy) error {
func SetInVariance(i int) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if i < 7 && i > -7 {
c.inVariance = i
c.Conf.InVariance = int(i)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -239,7 +239,7 @@ func SetInVariance(i int) func(*SAMMultiProxy) error {
func SetOutVariance(i int) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if i < 7 && i > -7 {
c.outVariance = i
c.Conf.OutVariance = int(i)
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -250,7 +250,7 @@ func SetOutVariance(i int) func(*SAMMultiProxy) error {
func SetInQuantity(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u <= 16 {
c.inQuantity = u
c.Conf.InQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -261,7 +261,7 @@ func SetInQuantity(u uint) func(*SAMMultiProxy) error {
func SetOutQuantity(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u <= 16 {
c.outQuantity = u
c.Conf.OutQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -272,7 +272,7 @@ func SetOutQuantity(u uint) func(*SAMMultiProxy) error {
func SetInBackups(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u < 6 {
c.inBackups = u
c.Conf.InBackupQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -283,7 +283,7 @@ func SetInBackups(u uint) func(*SAMMultiProxy) error {
func SetOutBackups(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u < 6 {
c.outBackups = u
c.Conf.OutBackupQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -293,7 +293,7 @@ func SetOutBackups(u uint) func(*SAMMultiProxy) error {
//SetUnpublished tells the router to not publish the client leaseset
func SetUnpublished(b bool) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.dontPublishLease = b
c.Conf.Client = b
return nil
}
}
@@ -301,7 +301,7 @@ func SetUnpublished(b bool) func(*SAMMultiProxy) error {
//SetEncrypt tells the router to use an encrypted leaseset
func SetEncrypt(b bool) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.encryptLease = b
c.Conf.EncryptLeaseSet = b
return nil
}
}
@@ -309,7 +309,7 @@ func SetEncrypt(b bool) func(*SAMMultiProxy) error {
//SetReduceIdle sets the created tunnels to be reduced during extended idle time to avoid excessive resource usage
func SetReduceIdle(b bool) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.reduceIdle = b
c.Conf.ReduceIdle = b
return nil
}
}
@@ -318,7 +318,7 @@ func SetReduceIdle(b bool) func(*SAMMultiProxy) error {
func SetReduceIdleTime(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u > 299999 {
c.reduceIdleTime = u
c.Conf.ReduceIdleTime = int(u)
return nil
}
return fmt.Errorf("Invalid reduce idle time %v", u)
@@ -329,7 +329,7 @@ func SetReduceIdleTime(u uint) func(*SAMMultiProxy) error {
func SetReduceIdleQuantity(u uint) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
if u < 5 {
c.reduceIdleQuantity = u
c.Conf.ReduceIdleQuantity = int(u)
return nil
}
return fmt.Errorf("Invalid reduced tunnel quantity %v", u)
@@ -339,7 +339,7 @@ func SetReduceIdleQuantity(u uint) func(*SAMMultiProxy) error {
//SetCompression sets the tunnels to close after a specific amount of time
func SetCompression(b bool) func(*SAMMultiProxy) error {
return func(c *SAMMultiProxy) error {
c.compression = b
c.Conf.UseCompression = b
return nil
}
}
@@ -354,75 +354,75 @@ func SetProxyMode(b bool) func(*SAMMultiProxy) error {
//return the inbound length as a string.
func (c *SAMMultiProxy) inlength() string {
return fmt.Sprintf("inbound.length=%d", c.inLength)
return fmt.Sprintf("inbound.length=%d", c.Conf.InLength)
}
//return the outbound length as a string.
func (c *SAMMultiProxy) outlength() string {
return fmt.Sprintf("outbound.length=%d", c.outLength)
return fmt.Sprintf("outbound.length=%d", c.Conf.OutLength)
}
//return the inbound length variance as a string.
func (c *SAMMultiProxy) invariance() string {
return fmt.Sprintf("inbound.lengthVariance=%d", c.inVariance)
return fmt.Sprintf("inbound.lengthVariance=%d", c.Conf.InVariance)
}
//return the outbound length variance as a string.
func (c *SAMMultiProxy) outvariance() string {
return fmt.Sprintf("outbound.lengthVariance=%d", c.outVariance)
return fmt.Sprintf("outbound.lengthVariance=%d", c.Conf.OutVariance)
}
//return the inbound tunnel quantity as a string.
func (c *SAMMultiProxy) inquantity() string {
return fmt.Sprintf("inbound.quantity=%d", c.inQuantity)
return fmt.Sprintf("inbound.quantity=%d", c.Conf.InQuantity)
}
//return the outbound tunnel quantity as a string.
func (c *SAMMultiProxy) outquantity() string {
return fmt.Sprintf("outbound.quantity=%d", c.outQuantity)
return fmt.Sprintf("outbound.quantity=%d", c.Conf.OutQuantity)
}
//return the inbound tunnel quantity as a string.
func (c *SAMMultiProxy) inbackups() string {
return fmt.Sprintf("inbound.backupQuantity=%d", c.inQuantity)
return fmt.Sprintf("inbound.backupQuantity=%d", c.Conf.InQuantity)
}
//return the outbound tunnel quantity as a string.
func (c *SAMMultiProxy) outbackups() string {
return fmt.Sprintf("outbound.backupQuantity=%d", c.outQuantity)
return fmt.Sprintf("outbound.backupQuantity=%d", c.Conf.OutQuantity)
}
func (c *SAMMultiProxy) encryptlease() string {
if c.encryptLease {
if c.Conf.EncryptLeaseSet {
return "i2cp.encryptLeaseSet=true"
}
return "i2cp.encryptLeaseSet=false"
}
func (c *SAMMultiProxy) dontpublishlease() string {
if c.dontPublishLease {
if c.Conf.Client {
return "i2cp.dontPublishLeaseSet=true"
}
return "i2cp.dontPublishLeaseSet=false"
}
func (c *SAMMultiProxy) reduceonidle() string {
if c.reduceIdle {
if c.Conf.ReduceIdle {
return "i2cp.reduceOnIdle=true"
}
return "i2cp.reduceOnIdle=false"
}
func (c *SAMMultiProxy) reduceidletime() string {
return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.reduceIdleTime)
return fmt.Sprintf("i2cp.reduceIdleTime=%d", c.Conf.ReduceIdleTime)
}
func (c *SAMMultiProxy) reduceidlecount() string {
return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.reduceIdleQuantity)
return fmt.Sprintf("i2cp.reduceIdleQuantity=%d", c.Conf.ReduceIdleQuantity)
}
func (c *SAMMultiProxy) usecompresion() string {
if c.compression {
if c.Conf.UseCompression {
return "i2cp.gzip=true"
}
return "i2cp.gzip=false"

View File

@@ -34,32 +34,8 @@ type samClient struct {
}
type SAMMultiProxy struct {
clients map[string]*samClient
Hasher *hashhash.Hasher
tunName string
sigType string
proxyHost string
proxyPort string
SamHost string
SamPort string
controlHost string
controlPort string
destination string
keyspath string
inLength uint
outLength uint
inVariance int
outVariance int
inQuantity uint
outQuantity uint
inBackups uint
outBackups uint
dontPublishLease bool
encryptLease bool
reduceIdle bool
reduceIdleTime uint
reduceIdleQuantity uint
compression bool
clients map[string]*samClient
Hasher *hashhash.Hasher
Conf *i2ptunconf.Conf
@@ -110,7 +86,7 @@ func (f *SAMMultiProxy) GetType() string {
}
func (f *SAMMultiProxy) ID() string {
return f.tunName
return f.Conf.TunName
}
func (p *SAMMultiProxy) Keys() i2pkeys.I2PKeys {
@@ -153,7 +129,7 @@ func (p *SAMMultiProxy) Search(search string) string {
}
func (p *SAMMultiProxy) Target() string {
return p.proxyHost + ":" + p.proxyPort
return p.Conf.TargetHost + ":" + p.Conf.TargetPort
}
func (p *SAMMultiProxy) Base32() string {
@@ -174,7 +150,7 @@ func (p *SAMMultiProxy) Base64() string {
}
func (p *SAMMultiProxy) Serve() error {
ln, err := net.Listen("tcp", p.proxyHost+":"+p.proxyPort)
ln, err := net.Listen("tcp", p.Conf.TargetHost+":"+p.Conf.TargetPort)
if err != nil {
return err
}
@@ -250,27 +226,27 @@ func (p *SAMMultiProxy) freshSAMClient(key string) (*samClient, error) {
func (p *SAMMultiProxy) freshGoSAMClient() (*goSam.Client, error) {
return goSam.NewClientFromOptions(
goSam.SetHost(p.SamHost),
goSam.SetPort(p.SamPort),
goSam.SetUnpublished(p.dontPublishLease),
goSam.SetInLength(p.inLength),
goSam.SetOutLength(p.outLength),
goSam.SetInQuantity(p.inQuantity),
goSam.SetOutQuantity(p.outQuantity),
goSam.SetInBackups(p.inBackups),
goSam.SetOutBackups(p.outBackups),
goSam.SetReduceIdle(p.reduceIdle),
goSam.SetReduceIdleTime(p.reduceIdleTime),
goSam.SetReduceIdleQuantity(p.reduceIdleQuantity),
goSam.SetCompression(p.compression),
goSam.SetHost(p.Conf.SamHost),
goSam.SetPort(p.Conf.SamPort),
goSam.SetUnpublished(p.Conf.Client),
goSam.SetInLength(uint(p.Conf.InLength)),
goSam.SetOutLength(uint(p.Conf.OutLength)),
goSam.SetInQuantity(uint(p.Conf.InQuantity)),
goSam.SetOutQuantity(uint(p.Conf.OutQuantity)),
goSam.SetInBackups(uint(p.Conf.InBackupQuantity)),
goSam.SetOutBackups(uint(p.Conf.OutBackupQuantity)),
goSam.SetReduceIdle(p.Conf.ReduceIdle),
goSam.SetReduceIdleTime(uint(p.Conf.ReduceIdleTime)),
goSam.SetReduceIdleQuantity(uint(p.Conf.ReduceIdleQuantity)),
goSam.SetCompression(p.Conf.UseCompression),
goSam.SetDebug(p.debug),
goSam.SetLocalDestination(p.destination),
goSam.SetLocalDestination(p.Conf.ClientDest),
)
}
//return the combined host:port of the SAM bridge
func (p *SAMMultiProxy) samaddr() string {
return fmt.Sprintf("%s:%s", p.SamHost, p.SamPort)
return fmt.Sprintf("%s:%s", p.Conf.SamHost, p.Conf.SamPort)
}
func (p *SAMMultiProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
@@ -286,7 +262,7 @@ func (p *SAMMultiProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
}
if !strings.HasSuffix(req.URL.Host, ".i2p") {
if req.URL.Host == p.controlHost+":"+p.controlPort {
if req.URL.Host == p.Conf.ControlHost+":"+p.Conf.ControlPort {
p.reset(wr, req)
return
}
@@ -309,10 +285,10 @@ func (p *SAMMultiProxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
}
func (p *SAMMultiProxy) reset(wr http.ResponseWriter, req *http.Request) {
plog("Validating control access from", req.RemoteAddr, p.controlHost+":"+p.controlPort)
if strings.SplitN(req.RemoteAddr, ":", 2)[0] == p.controlHost {
plog("Validated control access from", req.RemoteAddr, p.controlHost+":"+p.controlPort)
resp, err := http.Get("http://" + p.controlHost + ":" + p.controlPort)
plog("Validating control access from", req.RemoteAddr, p.Conf.ControlHost+":"+p.Conf.ControlPort)
if strings.SplitN(req.RemoteAddr, ":", 2)[0] == p.Conf.ControlHost {
plog("Validated control access from", req.RemoteAddr, p.Conf.ControlHost+":"+p.Conf.ControlPort)
resp, err := http.Get("http://" + p.Conf.ControlHost + ":" + p.Conf.ControlPort)
if err == nil {
wr.Header().Set("Content-Type", "text/html; charset=utf-8")
wr.Header().Set("Access-Control-Allow-Origin", "*")
@@ -377,18 +353,18 @@ func (f *SAMMultiProxy) Up() bool {
}
func (p *SAMMultiProxy) Save() string {
if p.keyspath != "invalid.tunkey" {
if _, err := os.Stat(p.keyspath); os.IsNotExist(err) {
if p.Conf.KeyFilePath != "invalid.tunkey" {
if _, err := os.Stat(p.Conf.KeyFilePath); os.IsNotExist(err) {
if p.findClient(p.recent).goSam != nil {
if p.findClient(p.recent).goSam.Destination() != "" {
ioutil.WriteFile(p.keyspath, []byte(p.findClient(p.recent).goSam.Destination()), 0644)
p.destination = p.findClient(p.recent).goSam.Destination()
ioutil.WriteFile(p.Conf.KeyFilePath, []byte(p.findClient(p.recent).goSam.Destination()), 0644)
p.Conf.ClientDest = p.findClient(p.recent).goSam.Destination()
return p.findClient(p.recent).goSam.Destination()
}
}
} else {
if keys, err := ioutil.ReadFile(p.keyspath); err == nil {
p.destination = string(keys)
if keys, err := ioutil.ReadFile(p.Conf.KeyFilePath); err == nil {
p.Conf.ClientDest = string(keys)
return string(keys)
}
}
@@ -398,24 +374,24 @@ func (p *SAMMultiProxy) Save() string {
func (handler *SAMMultiProxy) Load() (samtunnel.SAMTunnel, error) {
var err error
handler.destination = handler.Save()
handler.Conf.ClientDest = handler.Save()
handler.clients["general"] = &samClient{}
handler.clients["general"].goSam, err = goSam.NewClientFromOptions(
goSam.SetHost(handler.SamHost),
goSam.SetPort(handler.SamPort),
goSam.SetUnpublished(handler.dontPublishLease),
goSam.SetInLength(handler.inLength),
goSam.SetOutLength(handler.outLength),
goSam.SetInQuantity(handler.inQuantity),
goSam.SetOutQuantity(handler.outQuantity),
goSam.SetInBackups(handler.inBackups),
goSam.SetOutBackups(handler.outBackups),
goSam.SetReduceIdle(handler.reduceIdle),
goSam.SetReduceIdleTime(handler.reduceIdleTime),
goSam.SetReduceIdleQuantity(handler.reduceIdleQuantity),
goSam.SetCompression(handler.compression),
goSam.SetHost(handler.Conf.SamHost),
goSam.SetPort(handler.Conf.SamPort),
goSam.SetUnpublished(handler.Conf.Client),
goSam.SetInLength(uint(handler.Conf.InLength)),
goSam.SetOutLength(uint(handler.Conf.OutLength)),
goSam.SetInQuantity(uint(handler.Conf.InQuantity)),
goSam.SetOutQuantity(uint(handler.Conf.OutQuantity)),
goSam.SetInBackups(uint(handler.Conf.InBackupQuantity)),
goSam.SetOutBackups(uint(handler.Conf.OutBackupQuantity)),
goSam.SetReduceIdle(handler.Conf.ReduceIdle),
goSam.SetReduceIdleTime(uint(handler.Conf.ReduceIdleTime)),
goSam.SetReduceIdleQuantity(uint(handler.Conf.ReduceIdleQuantity)),
goSam.SetCompression(handler.Conf.UseCompression),
goSam.SetDebug(handler.debug),
goSam.SetLocalDestination(handler.destination),
goSam.SetLocalDestination(handler.Conf.ClientDest),
)
if err != nil {
return nil, err
@@ -432,30 +408,9 @@ func (handler *SAMMultiProxy) Load() (samtunnel.SAMTunnel, error) {
func NewHttpProxy(opts ...func(*SAMMultiProxy) error) (*SAMMultiProxy, error) {
var handler SAMMultiProxy
handler.SamHost = "127.0.0.1"
handler.SamPort = "7656"
handler.controlHost = "127.0.0.1"
handler.controlPort = "7951"
handler.proxyHost = "127.0.0.1"
handler.proxyPort = "7950"
handler.inLength = 2
handler.outLength = 2
handler.inVariance = 0
handler.outVariance = 0
handler.inQuantity = 1
handler.outQuantity = 1
handler.inBackups = 1
handler.outBackups = 1
handler.dontPublishLease = true
handler.encryptLease = false
handler.reduceIdle = false
handler.reduceIdleTime = 2000000
handler.reduceIdleQuantity = 1
handler.useOutProxy = false
handler.compression = true
handler.tunName = "0"
handler.keyspath = "invalid.tunkey"
handler.destination = ""
handler.Conf = &i2ptunconf.Conf{}
handler.Conf.SamHost = "127.0.0.1"
handler.Conf.SamPort = "7656"
handler.clients = make(map[string]*samClient)
handler.recent = "general"
handler.aggressive = false