14 Commits

Author SHA1 Message Date
idk
ccc29b5e66 purge redundant config items! 2019-08-31 16:55:22 -04:00
idk
36655256c3 fix client control panel in webui 2019-08-31 15:12:08 -04:00
idk
efccedaafb next part of the migration to a single standardized config struct 2019-08-31 13:22:32 -04:00
idk
a75baf9db7 next part of the migration to a single standardized config struct 2019-08-31 13:22:12 -04:00
idk
d67c0c0e31 move the helpers 2019-08-31 03:12:54 -04:00
idk
e88e3a0a6b move the helpers 2019-08-31 03:12:09 -04:00
idk
f530ccd37f Fix the helpers 2019-08-31 03:05:50 -04:00
idk
afdc44afc6 Fix the helpers 2019-08-31 03:02:13 -04:00
idk
6b873f4fc2 update the interface 2019-08-31 02:50:11 -04:00
idk
c427d576cb more prep to reduce the amount of repeated configuration code 2019-08-31 00:13:20 -04:00
idk
383fafea25 more prep to reduce the amount of repeated configuration code 2019-08-31 00:13:10 -04:00
idk
9254afc776 move config stuff around 2019-08-25 20:32:50 -04:00
idk
29c48f906c begin config cleanup 2019-08-25 19:21:11 -04:00
idk
38a4e1b3ad begin config cleanup 2019-08-25 19:20:54 -04:00
27 changed files with 1172 additions and 1128 deletions

View File

@@ -7,6 +7,7 @@ network = host
samhost = sam-host
samport = 7656
args = -r
USER_GH=eyedeekay
PREFIX := /
VAR := var/
@@ -16,7 +17,7 @@ LOG := log/
ETC := etc/
USR := usr/
LOCAL := local/
VERSION := 0.1
VERSION := 0.32.04
GO111MODULE=on
@@ -36,7 +37,7 @@ fix-debian:
find ./debian -type f -exec sed -i 's|eyedeekay@safe-mail.net|hankhill19580@gmail.com|g' {} \;
try:
cd etc/samcatd/ && ../../bin/samcatd -f tunnels.ini
./bin/samcatd -f etc/samcatd/tunnels.ini
test: test-keys test-ntcp test-ssu test-config test-manager
@@ -222,3 +223,10 @@ tar:
--exclude .go \
--exclude bin \
-cJvf ../$(packagename)_$(VERSION).orig.tar.xz .
tag:
gothub release -s $(GITHUB_TOKEN) -u $(USER_GH) -r $(packagename) -t v$(VERSION) -d "I2P Tunnel Management tool for Go applications"
sed:
sed -i 's|func(\*Conf)|func(samtunnel.SAMTunnel)|g' ./config/*.go
sed -i 's|func(c \*Conf)|func(c samtunnel.SAMTunnel)|g' ./config/*.go

View File

@@ -1,6 +1,8 @@
package i2ptunconf
import "strings"
import (
"strings"
)
// GetAccessListType takes an argument and a default. If the argument differs from the
// default, the argument is always returned. If the argument and default are

181
config/conf.go Normal file
View File

@@ -0,0 +1,181 @@
package i2ptunconf
import (
"fmt"
"strings"
)
import (
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam3/i2pkeys"
)
var err error
func (f *Conf) ID() string {
return f.TunName
}
func (f *Conf) Keys() i2pkeys.I2PKeys {
return f.LoadedKeys
}
func (f *Conf) Cleanup() {
}
func (f *Conf) GetType() string {
return f.Type
}
/*func (f *Conf) targetForPort443() string {
if f.TargetForPort443 != "" {
return "targetForPort.4443=" + f.TargetHost + ":" + f.TargetForPort443
}
return ""
}*/
func (f *Conf) print() []string {
lsk, lspk, lspsk := f.leasesetsettings()
return []string{
//f.targetForPort443(),
"inbound.length=" + fmt.Sprintf("%d", f.InLength),
"outbound.length=" + fmt.Sprintf("%d", f.OutLength),
"inbound.lengthVariance=" + fmt.Sprintf("%d", f.InVariance),
"outbound.lengthVariance=" + fmt.Sprintf("%d", f.OutVariance),
"inbound.backupQuantity=" + fmt.Sprintf("%d", f.InBackupQuantity),
"outbound.backupQuantity=" + fmt.Sprintf("%d", f.OutBackupQuantity),
"inbound.quantity=" + fmt.Sprintf("%d", f.InQuantity),
"outbound.quantity=" + fmt.Sprintf("%d", f.OutQuantity),
"inbound.allowZeroHop=" + fmt.Sprintf("%b", f.InAllowZeroHop),
"outbound.allowZeroHop=" + fmt.Sprintf("%b", f.OutAllowZeroHop),
"i2cp.fastRecieve=" + fmt.Sprintf("%b", f.FastRecieve),
"i2cp.gzip=" + fmt.Sprintf("%b", f.UseCompression),
"i2cp.reduceOnIdle=" + fmt.Sprintf("%b", f.ReduceIdle),
"i2cp.reduceIdleTime=" + fmt.Sprintf("%d", f.ReduceIdleTime),
"i2cp.reduceQuantity=" + fmt.Sprintf("%d", f.ReduceIdleQuantity),
"i2cp.closeOnIdle=" + fmt.Sprintf("%b", f.CloseIdle),
"i2cp.closeIdleTime=" + fmt.Sprintf("%d", f.CloseIdleTime),
"i2cp.messageReliability=" + f.MessageReliability,
"i2cp.encryptLeaseSet=" + fmt.Sprintf("%b", f.EncryptLeaseSet),
lsk, lspk, lspsk,
f.accesslisttype(),
f.accesslist(),
}
}
func (f *Conf) Props() map[string]string {
r := make(map[string]string)
print := f.print()
print = append(print, "base32="+f.Base32())
print = append(print, "base64="+f.Base64())
print = append(print, "base32words="+f.Base32Readable())
for _, prop := range print {
k, v := sfi2pkeys.Prop(prop)
r[k] = v
}
return r
}
func (f *Conf) Print() string {
var r string
r += "name=" + f.TunName + "\n"
r += "type=" + f.Type + "\n"
if f.Type == "http" {
r += "httpserver\n"
} else {
r += "ntcpserver\n"
}
for _, s := range f.print() {
r += s + "\n"
}
return strings.Replace(r, "\n\n", "\n", -1)
}
func (f *Conf) Search(search string) string {
terms := strings.Split(search, ",")
if search == "" {
return f.Print()
}
for _, value := range terms {
if !strings.Contains(f.Print(), value) {
return ""
}
}
return f.Print()
}
/*
func (f *Conf) accesslisttype() string {
if f.accessListType == "whitelist" {
return "i2cp.enableAccessList=true"
} else if f.accessListType == "blacklist" {
return "i2cp.enableBlackList=true"
} else if f.accessListType == "none" {
return ""
}
return ""
}
func (f *Conf) accesslist() string {
if f.accessListType != "" && len(f.accessList) > 0 {
r := ""
for _, s := range f.accessList {
r += s + ","
}
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
}
return ""
}
*/
func (f *Conf) leasesetsettings() (string, string, string) {
var r, s, t string
if f.LeaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.LeaseSetKey
}
if f.LeaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.LeaseSetPrivateKey
}
if f.LeaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.LeaseSetPrivateSigningKey
}
return r, s, t
}
// Target returns the host:port of the local service you want to forward to i2p
func (f *Conf) Target() string {
return f.TargetHost + ":" + f.TargetPort
}
func (f *Conf) sam() string {
return f.SamHost + ":" + f.SamPort
}
//Base32 returns the base32 address where the local service is being forwarded
func (f *Conf) Base32() string {
return f.LoadedKeys.Addr().Base32()
}
//Base32Readable will always be an empty string when used here.
func (f *Conf) Base32Readable() string {
return ""
}
//Base64 returns the base64 address where the local service is being forwarded
func (f *Conf) Base64() string {
return f.LoadedKeys.Addr().Base64()
}
//Serve starts the SAM connection and and forwards the local host:port to i2p
func (f *Conf) Serve() error {
return nil
}
func (f *Conf) Up() bool {
return false
}
//Close shuts the whole thing down.
func (f *Conf) Close() error {
return nil
}

20
config/config-options.go Normal file
View File

@@ -0,0 +1,20 @@
package i2ptunconf
//Option is a Conf Option
type Option func(*Conf) error
//SetTargetForPort sets the port of the Conf's SAM bridge using a string
/*func SetTargetForPort443(s string) func(samtunnel.SAMTunnel) error {
return func(c samtunnel.SAMTunnel) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetForPort443 = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
*/

View File

@@ -1,5 +1,7 @@
package i2ptunconf
//
// GetDir takes an argument and a default. If the argument differs from the
// default, the argument is always returned. If the argument and default are
// the same and the key exists, the key is returned. If the key is absent, the

View File

@@ -47,6 +47,6 @@ func (c *Conf) SetTunName(label ...string) {
if v, ok := c.Get("keys", label...); ok {
c.TunName = v
} else {
c.TunName = "fowarder"
c.TunName = "forwarder"
}
}

View File

@@ -10,6 +10,7 @@ import (
)
import (
"github.com/eyedeekay/sam3/i2pkeys"
"github.com/zieckey/goini"
)
@@ -62,10 +63,11 @@ type Conf struct {
exists bool
UserName string
Password string
LoadedKeys i2pkeys.I2PKeys
}
// Print returns and prints a formatted list of configured tunnel settings.
func (c *Conf) Print() []string {
// PrintSlice returns and prints a formatted list of configured tunnel settings.
func (c *Conf) PrintSlice() []string {
confstring := []string{
c.SignatureType(),
"inbound.length=" + strconv.Itoa(c.InLength),
@@ -94,7 +96,7 @@ func (c *Conf) Print() []string {
c.lsspk(),
}
log.Println(confstring)
log.Println("Tunnel:", c.TunName, "using config:", confstring)
return confstring
}

View File

@@ -1,224 +0,0 @@
package i2ptunconf
import (
"github.com/eyedeekay/httptunnel"
"github.com/eyedeekay/httptunnel/multiproxy"
"github.com/eyedeekay/sam-forwarder/tcp"
"github.com/eyedeekay/sam-forwarder/udp"
)
func NewSAMHTTPClientFromConf(config *Conf) (*i2phttpproxy.SAMHTTPProxy, error) {
if config != nil {
return i2phttpproxy.NewHttpProxy(
i2phttpproxy.SetName(config.TunName),
i2phttpproxy.SetKeysPath(config.KeyFilePath),
i2phttpproxy.SetHost(config.SamHost),
i2phttpproxy.SetPort(config.SamPort),
i2phttpproxy.SetProxyAddr(config.TargetHost+":"+config.TargetPort),
i2phttpproxy.SetControlHost(config.ControlHost),
i2phttpproxy.SetControlPort(config.ControlPort),
i2phttpproxy.SetInLength(uint(config.InLength)),
i2phttpproxy.SetOutLength(uint(config.OutLength)),
i2phttpproxy.SetInQuantity(uint(config.InQuantity)),
i2phttpproxy.SetOutQuantity(uint(config.OutQuantity)),
i2phttpproxy.SetInBackups(uint(config.InBackupQuantity)),
i2phttpproxy.SetOutBackups(uint(config.OutBackupQuantity)),
i2phttpproxy.SetInVariance(config.InVariance),
i2phttpproxy.SetOutVariance(config.OutVariance),
i2phttpproxy.SetUnpublished(config.Client),
i2phttpproxy.SetReduceIdle(config.ReduceIdle),
i2phttpproxy.SetCompression(config.UseCompression),
i2phttpproxy.SetReduceIdleTime(uint(config.ReduceIdleTime)),
i2phttpproxy.SetReduceIdleQuantity(uint(config.ReduceIdleQuantity)),
i2phttpproxy.SetCloseIdle(config.CloseIdle),
i2phttpproxy.SetCloseIdleTime(uint(config.CloseIdleTime)),
)
}
return nil, nil
}
// NewSAMClientForwarderFromConfig generates a new SAMForwarder from a config file
func NewSAMHTTPClientFromConfig(iniFile, SamHost, SamPort string, label ...string) (*i2phttpproxy.SAMHTTPProxy, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMHTTPClientFromConf(config)
}
return nil, nil
}
func NewSAMBrowserClientFromConf(config *Conf) (*i2pbrowserproxy.SAMMultiProxy, error) {
if config != nil {
return i2pbrowserproxy.NewHttpProxy(
i2pbrowserproxy.SetName(config.TunName),
i2pbrowserproxy.SetKeysPath(config.KeyFilePath),
i2pbrowserproxy.SetHost(config.SamHost),
i2pbrowserproxy.SetPort(config.SamPort),
i2pbrowserproxy.SetProxyAddr(config.TargetHost+":"+config.TargetPort),
i2pbrowserproxy.SetControlHost(config.ControlHost),
i2pbrowserproxy.SetControlPort(config.ControlPort),
i2pbrowserproxy.SetInLength(uint(config.InLength)),
i2pbrowserproxy.SetOutLength(uint(config.OutLength)),
i2pbrowserproxy.SetInQuantity(uint(config.InQuantity)),
i2pbrowserproxy.SetOutQuantity(uint(config.OutQuantity)),
i2pbrowserproxy.SetInBackups(uint(config.InBackupQuantity)),
i2pbrowserproxy.SetOutBackups(uint(config.OutBackupQuantity)),
i2pbrowserproxy.SetInVariance(config.InVariance),
i2pbrowserproxy.SetOutVariance(config.OutVariance),
i2pbrowserproxy.SetUnpublished(config.Client),
i2pbrowserproxy.SetReduceIdle(config.ReduceIdle),
i2pbrowserproxy.SetCompression(config.UseCompression),
i2pbrowserproxy.SetReduceIdleTime(uint(config.ReduceIdleTime)),
i2pbrowserproxy.SetReduceIdleQuantity(uint(config.ReduceIdleQuantity)),
//i2pbrowserproxy.SetCloseIdle(config.CloseIdle),
//i2pbrowserproxy.SetCloseIdleTime(uint(config.CloseIdleTime)),
)
}
return nil, nil
}
func NewSAMBrowserClientFromConfig(iniFile, SamHost, SamPort string, label ...string) (*i2pbrowserproxy.SAMMultiProxy, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMBrowserClientFromConf(config)
}
return nil, nil
}
// NewSAMClientForwarderFromConf generates a SAMforwarder from *i2ptunconf.Conf
func NewSAMClientForwarderFromConf(config *Conf) (*samforwarder.SAMClientForwarder, error) {
if config != nil {
return 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),
)
}
return nil, nil
}
// NewSAMClientForwarderFromConfig generates a new SAMForwarder from a config file
func NewSAMClientForwarderFromConfig(iniFile, SamHost, SamPort string, label ...string) (*samforwarder.SAMClientForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMClientForwarderFromConf(config)
}
return nil, nil
}
// NewSAMSSUClientForwarderFromConf generates a SAMSSUforwarder from *i2ptunconf.Conf
func NewSAMSSUClientForwarderFromConf(config *Conf) (*samforwarderudp.SAMSSUClientForwarder, error) {
if config != nil {
return samforwarderudp.NewSAMSSUClientForwarderFromOptions(
samforwarderudp.SetClientSaveFile(config.SaveFile),
samforwarderudp.SetClientFilePath(config.SaveDirectory),
samforwarderudp.SetClientHost(config.TargetHost),
samforwarderudp.SetClientPort(config.TargetPort),
samforwarderudp.SetClientSAMHost(config.SamHost),
samforwarderudp.SetClientSAMPort(config.SamPort),
samforwarderudp.SetClientSigType(config.SigType),
samforwarderudp.SetClientName(config.TunName),
samforwarderudp.SetClientInLength(config.InLength),
samforwarderudp.SetClientOutLength(config.OutLength),
samforwarderudp.SetClientInVariance(config.InVariance),
samforwarderudp.SetClientOutVariance(config.OutVariance),
samforwarderudp.SetClientInQuantity(config.InQuantity),
samforwarderudp.SetClientOutQuantity(config.OutQuantity),
samforwarderudp.SetClientInBackups(config.InBackupQuantity),
samforwarderudp.SetClientOutBackups(config.OutBackupQuantity),
samforwarderudp.SetClientEncrypt(config.EncryptLeaseSet),
samforwarderudp.SetClientLeaseSetKey(config.LeaseSetKey),
samforwarderudp.SetClientLeaseSetPrivateKey(config.LeaseSetPrivateKey),
samforwarderudp.SetClientLeaseSetPrivateSigningKey(config.LeaseSetPrivateSigningKey),
samforwarderudp.SetClientAllowZeroIn(config.InAllowZeroHop),
samforwarderudp.SetClientAllowZeroOut(config.OutAllowZeroHop),
samforwarderudp.SetClientFastRecieve(config.FastRecieve),
samforwarderudp.SetClientCompress(config.UseCompression),
samforwarderudp.SetClientReduceIdle(config.ReduceIdle),
samforwarderudp.SetClientReduceIdleTimeMs(config.ReduceIdleTime),
samforwarderudp.SetClientReduceIdleQuantity(config.ReduceIdleQuantity),
samforwarderudp.SetClientCloseIdle(config.CloseIdle),
samforwarderudp.SetClientCloseIdleTimeMs(config.CloseIdleTime),
samforwarderudp.SetClientAccessListType(config.AccessListType),
samforwarderudp.SetClientAccessList(config.AccessList),
samforwarderudp.SetClientMessageReliability(config.MessageReliability),
samforwarderudp.SetClientPassword(config.KeyFilePath),
samforwarderudp.SetClientDestination(config.ClientDest),
)
}
return nil, nil
}
func NewSAMSSUClientForwarderFromConfig(iniFile, SamHost, SamPort string, label ...string) (*samforwarderudp.SAMSSUClientForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMSSUClientForwarderFromConf(config)
}
return nil, nil
}

View File

@@ -1,128 +0,0 @@
package i2ptunconf
import (
"github.com/eyedeekay/sam-forwarder/tcp"
"github.com/eyedeekay/sam-forwarder/udp"
)
// NewSAMForwarderFromConf generates a SAMforwarder from *i2ptunconf.Conf
func NewSAMForwarderFromConf(config *Conf) (*samforwarder.SAMForwarder, error) {
if config != nil {
return samforwarder.NewSAMForwarderFromOptions(
samforwarder.SetType(config.Type),
samforwarder.SetSaveFile(config.SaveFile),
samforwarder.SetFilePath(config.SaveDirectory),
samforwarder.SetHost(config.TargetHost),
samforwarder.SetPort(config.TargetPort),
samforwarder.SetSAMHost(config.SamHost),
samforwarder.SetSAMPort(config.SamPort),
samforwarder.SetSigType(config.SigType),
samforwarder.SetName(config.TunName),
samforwarder.SetInLength(config.InLength),
samforwarder.SetOutLength(config.OutLength),
samforwarder.SetInVariance(config.InVariance),
samforwarder.SetOutVariance(config.OutVariance),
samforwarder.SetInQuantity(config.InQuantity),
samforwarder.SetOutQuantity(config.OutQuantity),
samforwarder.SetInBackups(config.InBackupQuantity),
samforwarder.SetOutBackups(config.OutBackupQuantity),
samforwarder.SetEncrypt(config.EncryptLeaseSet),
samforwarder.SetLeaseSetKey(config.LeaseSetKey),
samforwarder.SetLeaseSetPrivateKey(config.LeaseSetPrivateKey),
samforwarder.SetLeaseSetPrivateSigningKey(config.LeaseSetPrivateSigningKey),
samforwarder.SetAllowZeroIn(config.InAllowZeroHop),
samforwarder.SetAllowZeroOut(config.OutAllowZeroHop),
samforwarder.SetFastRecieve(config.FastRecieve),
samforwarder.SetCompress(config.UseCompression),
samforwarder.SetReduceIdle(config.ReduceIdle),
samforwarder.SetReduceIdleTimeMs(config.ReduceIdleTime),
samforwarder.SetReduceIdleQuantity(config.ReduceIdleQuantity),
samforwarder.SetCloseIdle(config.CloseIdle),
samforwarder.SetCloseIdleTimeMs(config.CloseIdleTime),
samforwarder.SetAccessListType(config.AccessListType),
samforwarder.SetAccessList(config.AccessList),
samforwarder.SetMessageReliability(config.MessageReliability),
samforwarder.SetKeyFile(config.KeyFilePath),
//samforwarder.SetTargetForPort443(config.TargetForPort443),
)
}
return nil, nil
}
// NewSAMForwarderFromConfig generates a new SAMForwarder from a config file
func NewSAMForwarderFromConfig(iniFile, SamHost, SamPort string, label ...string) (*samforwarder.SAMForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMForwarderFromConf(config)
}
return nil, nil
}
// NewSAMSSUForwarderFromConf generates a SAMSSUforwarder from *i2ptunconf.Conf
func NewSAMSSUForwarderFromConf(config *Conf) (*samforwarderudp.SAMSSUForwarder, error) {
if config != nil {
return samforwarderudp.NewSAMSSUForwarderFromOptions(
samforwarderudp.SetSaveFile(config.SaveFile),
samforwarderudp.SetFilePath(config.SaveDirectory),
samforwarderudp.SetHost(config.TargetHost),
samforwarderudp.SetPort(config.TargetPort),
samforwarderudp.SetSAMHost(config.SamHost),
samforwarderudp.SetSAMPort(config.SamPort),
samforwarderudp.SetSigType(config.SigType),
samforwarderudp.SetName(config.TunName),
samforwarderudp.SetInLength(config.InLength),
samforwarderudp.SetOutLength(config.OutLength),
samforwarderudp.SetInVariance(config.InVariance),
samforwarderudp.SetOutVariance(config.OutVariance),
samforwarderudp.SetInQuantity(config.InQuantity),
samforwarderudp.SetOutQuantity(config.OutQuantity),
samforwarderudp.SetInBackups(config.InBackupQuantity),
samforwarderudp.SetOutBackups(config.OutBackupQuantity),
samforwarderudp.SetEncrypt(config.EncryptLeaseSet),
samforwarderudp.SetLeaseSetKey(config.LeaseSetKey),
samforwarderudp.SetLeaseSetPrivateKey(config.LeaseSetPrivateKey),
samforwarderudp.SetLeaseSetPrivateSigningKey(config.LeaseSetPrivateSigningKey),
samforwarderudp.SetAllowZeroIn(config.InAllowZeroHop),
samforwarderudp.SetAllowZeroOut(config.OutAllowZeroHop),
samforwarderudp.SetFastRecieve(config.FastRecieve),
samforwarderudp.SetCompress(config.UseCompression),
samforwarderudp.SetReduceIdle(config.ReduceIdle),
samforwarderudp.SetReduceIdleTimeMs(config.ReduceIdleTime),
samforwarderudp.SetReduceIdleQuantity(config.ReduceIdleQuantity),
samforwarderudp.SetCloseIdle(config.CloseIdle),
samforwarderudp.SetCloseIdleTimeMs(config.CloseIdleTime),
samforwarderudp.SetAccessListType(config.AccessListType),
samforwarderudp.SetAccessList(config.AccessList),
samforwarderudp.SetMessageReliability(config.MessageReliability),
samforwarderudp.SetKeyFile(config.KeyFilePath),
)
}
return nil, nil
}
// NewSAMSSUForwarderFromConfig generates a new SAMSSUForwarder from a config file
func NewSAMSSUForwarderFromConfig(iniFile, SamHost, SamPort string, label ...string) (*samforwarderudp.SAMSSUForwarder, error) {
if iniFile != "none" {
config, err := NewI2PTunConf(iniFile, label...)
if err != nil {
return nil, err
}
if SamHost != "" && SamHost != "127.0.0.1" && SamHost != "localhost" {
config.SamHost = config.GetSAMHost(SamHost, config.SamHost)
}
if SamPort != "" && SamPort != "7656" {
config.SamPort = config.GetSAMPort(SamPort, config.SamPort)
}
return NewSAMSSUForwarderFromConf(config)
}
return nil, nil
}

View File

@@ -8,7 +8,7 @@ import (
// default, the argument is always returned. If the argument and default are
// the same and the key exists, the key is returned. If the key is absent, the
// default is returned.
func (c *Conf) GetType(argc, argu, argh bool, def string, label ...string) string {
func (c *Conf) GetTypes(argc, argu, argh bool, def string, label ...string) string {
var typ string
if argu {
typ += "udp"
@@ -63,10 +63,33 @@ func (c *Conf) SetType(label ...string) {
if strings.Contains(v, "client") {
c.Client = true
}
if c.Type == "server" || c.Type == "http" || c.Type == "client" || c.Type == "httpclient" || c.Type == "udpserver" || c.Type == "udpclient" || c.Type == "kcpclient" || c.Type == "kcpserver" {
switch c.Type {
case "server":
c.Type = v
case "http":
c.Type = v
case "client":
c.Type = v
case "httpclient":
c.Type = v
case "browserclient":
c.Type = v
case "udpserver":
c.Type = v
case "udpclient":
c.Type = v
case "vpnserver":
c.Type = v
case "vpnclient":
c.Type = v
case "kcpclient":
c.Type = v
case "kcpserver":
c.Type = v
default:
c.Type = "browserclient"
}
} else {
c.Type = "server"
c.Type = "browserclient"
}
}

20
go.mod
View File

@@ -3,24 +3,18 @@ module github.com/eyedeekay/sam-forwarder
go 1.12
require (
crawshaw.io/littleboss v0.0.0-20190317185602-8957d0aedcce
// github.com/RTradeLtd/go-anonvpn v0.0.0-20190914175831-ff09cd346471
github.com/boltdb/bolt v1.3.1 // indirect
github.com/boreq/friendlyhash v0.0.0-20190522010448-1ca64b3ca69e
github.com/eyedeekay/goSam v0.1.1-0.20190814204230-d4c9b8c57dd6 // indirect
github.com/eyedeekay/httptunnel v0.0.0-20190814204746-0081636585cd
github.com/eyedeekay/httptunnel v0.0.0-20190831065052-9eab288b8a82
github.com/eyedeekay/portcheck v0.0.0-20190218044454-bb8718669680
github.com/eyedeekay/sam3 v0.0.0-20190730185140-f8d54526ea25
github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69
github.com/justinas/nosurf v0.0.0-20190416172904-05988550ea18
github.com/kr/pty v1.1.8 // indirect
github.com/zieckey/goini v0.0.0-20180118150432-0da17d361d26
github.com/zserge/lorca v0.1.8
github.com/zserge/webview v0.0.0-20180509070823-016c6ffd99f3
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
golang.org/x/tools v0.0.0-20190814214036-f60b6e7d83f4 // indirect
github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
golang.org/x/sys v0.0.0-20190830142957-1e83adbbebd0 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190830223141-573d9926052a // indirect
)
replace golang.org/x/lint v0.0.0 => github.com/golang/lint v0.0.0

View File

@@ -74,20 +74,24 @@ func (m *TunnelHandlerMux) Home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<!DOCTYPE html>\n")
fmt.Fprintf(w, "<html>\n")
fmt.Fprintf(w, "<head>\n")
fmt.Fprintf(w, " <title>")
fmt.Fprintf(w, "samcatd")
fmt.Fprintf(w, " </title>")
fmt.Fprintf(w, " <link rel=\"stylesheet\" href=\"/styles.css\">")
fmt.Fprintf(w, "</head>\n")
fmt.Fprintf(w, "<body>\n")
fmt.Fprintf(w, "<h1>\n")
w.Write([]byte(fmt.Sprintf("<a href=\"/index.html\">Welcome %s! you are serving %d tunnels. </a>\n", m.user, len(m.tunnels))))
fmt.Fprintf(w, "</h1>\n")
fmt.Fprintf(w, " <div id=\"toggleall\" class=\"global control\">\n")
fmt.Fprintf(w, " <a href=\"#\" onclick=\"toggle_visibility_class('%s');\">Show/Hide %s</a>\n", "prop", "all")
fmt.Fprintf(w, " </div>\n")
fmt.Fprintf(w, " <body>\n")
fmt.Fprintf(w, " <h1>\n")
w.Write([]byte(fmt.Sprintf(" <a href=\"/index.html\">Welcome %s! you are serving %d tunnels. </a>\n", m.user, len(m.tunnels))))
//fmt.Fprintf(w, "")
fmt.Fprintf(w, " </h1>\n")
fmt.Fprintf(w, " <div id=\"toggleall\" class=\"global control\">\n")
fmt.Fprintf(w, " <a href=\"#\" onclick=\"toggle_visibility_class('%s');\">Show/Hide %s</a>\n", "prop", "all")
fmt.Fprintf(w, " </div>\n")
for _, tunnel := range m.Tunnels() {
tunnel.ServeHTTP(w, r2)
}
fmt.Fprintf(w, " <script src=\"/scripts.js\"></script>\n")
fmt.Fprintf(w, "</body>\n")
fmt.Fprintf(w, " <script src=\"/scripts.js\"></script>\n")
fmt.Fprintf(w, " </body>\n")
fmt.Fprintf(w, "</html>\n")
}

View File

@@ -15,6 +15,13 @@ func DefaultCSS() string {
float: left;
overflow-wrap: break-word;
}
.tcpclient {
width: 63%;
min-height: 15%;
background-color: #2D4470;
float: left;
overflow-wrap: break-word;
}
.http {
width: 63%;
min-height: 15%;

View File

@@ -46,7 +46,7 @@ func PropSort(props map[string]string) []string {
return slice
}
func (t *TunnelHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
func (t *TunnelHandler) ControlForm(rw http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err == nil {
if action := req.PostFormValue("action"); action != "" {
var err error
@@ -87,6 +87,10 @@ func (t *TunnelHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
}
}
}
func (t *TunnelHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
t.ControlForm(rw, req)
if strings.HasSuffix(req.URL.Path, "color") {
fmt.Fprintf(rw, " <div id=\"%s\" class=\"%s\" >", t.SAMTunnel.ID(), t.SAMTunnel.GetType())

View File

@@ -0,0 +1,469 @@
package samtunnel
import (
"fmt"
"strconv"
)
//SetAccessListType tells the system to treat the accessList as a whitelist
func SetAccessListType(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if s == "whitelist" {
c.Config().AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.Config().AccessListType = "blacklist"
return nil
} else if s == "none" {
c.Config().AccessListType = ""
return nil
} else if s == "" {
c.Config().AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
}
}
//SetAccessList tells the system to treat the accessList as a whitelist
func SetAccessList(s []string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if len(s) > 0 {
for _, a := range s {
c.Config().AccessList = append(c.Config().AccessList, a)
}
return nil
}
return nil
}
}
//SetCompress tells clients to use compression
func SetCompress(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if b {
c.Config().UseCompression = b // "true"
return nil
}
c.Config().UseCompression = b // "false"
return nil
}
}
//SetFilePath sets the path to save the config file at.
func SetFilePath(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().FilePath = s
return nil
}
}
//SetControlHost sets the host of the service to forward
func SetControlHost(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().ControlHost = s
return nil
}
}
//SetControlPort sets the port of the service to forward
func SetControlPort(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid TCP Server Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.Config().ControlPort = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
//SetKeyFile sets
func SetKeyFile(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().KeyFilePath = s
return nil
}
}
//SetSaveFile tells the router to save the tunnel's keys long-term
func SetDestination(b string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().ClientDest = b
return nil
}
}
//SetTunnelHost is used for VPN endpoints
func SetTunnelHost(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().TunnelHost = s
return nil
}
}
//SetFastRecieve tells clients to recieve all messages as quicky as possible
func SetFastRecieve(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().FastRecieve = b
return nil
}
}
//SetHost sets the host of the service to forward
func SetHost(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().TargetHost = s
return nil
}
}
//SetPort sets the port of the service to forward
func SetPort(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid TCP Server Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.Config().TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
//SetEncrypt tells the router to use an encrypted leaseset
func SetEncrypt(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if b {
c.Config().EncryptLeaseSet = b //"true"
return nil
}
c.Config().EncryptLeaseSet = b //"false"
return nil
}
}
//SetLeaseSetKey sets the host of the Conf's SAM bridge
func SetLeaseSetKey(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().LeaseSetKey = s
return nil
}
}
//SetLeaseSetPrivateKey sets the host of the Conf's SAM bridge
func SetLeaseSetPrivateKey(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().LeaseSetPrivateKey = s
return nil
}
}
//SetLeaseSetPrivateSigningKey sets the host of the Conf's SAM bridge
func SetLeaseSetPrivateSigningKey(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().LeaseSetPrivateSigningKey = s
return nil
}
}
//SetInLength sets the number of hops inbound
func SetInLength(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u < 7 && u >= 0 {
c.Config().InLength = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
//SetOutLength sets the number of hops outbound
func SetOutLength(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u < 7 && u >= 0 {
c.Config().OutLength = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
}
}
//SetName sets the host of the Conf's SAM bridge
func SetName(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().TunName = s
return nil
}
}
//SetSaveFile tells the router to save the tunnel's keys long-term
func SetSaveFile(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().SaveFile = b
return nil
}
}
//SetPassword sets the host of the Conf's SAM bridge
func SetPassword(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().Password = s
return nil
}
}
//SetInQuantity sets the inbound tunnel quantity
func SetInQuantity(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u <= 16 && u > 0 {
c.Config().InQuantity = u //strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
}
}
//SetOutQuantity sets the outbound tunnel quantity
func SetOutQuantity(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u <= 16 && u > 0 {
c.Config().OutQuantity = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
}
}
//SetReduceIdle tells the connection to reduce it's tunnels during extended idle time.
func SetReduceIdle(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().ReduceIdle = b // "false"
return nil
}
}
//SetReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetReduceIdleTime(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().ReduceIdleTime = 300000
if u >= 6 {
c.Config().ReduceIdleTime = (u * 60) * 1000 // strconv.Itoa((u * 60) * 1000)
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u)
}
}
//SetReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetReduceIdleTimeMs(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().ReduceIdleTime = 300000
if u >= 300000 {
c.Config().ReduceIdleTime = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
}
}
//SetReduceIdleQuantity sets minimum number of tunnels to reduce to during idle time
func SetReduceIdleQuantity(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u < 5 {
c.Config().ReduceIdleQuantity = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
}
}
//SetCloseIdle tells the connection to close it's tunnels during extended idle time.
func SetCloseIdle(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().CloseIdle = b // "false"
return nil
}
}
//SetCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetCloseIdleTime(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().CloseIdleTime = 300000
if u >= 6 {
c.Config().CloseIdleTime = (u * 60) * 1000 // strconv.Itoa((u * 60) * 1000)
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
}
}
//SetCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetCloseIdleTimeMs(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().CloseIdleTime = 300000
if u >= 300000 {
c.Config().CloseIdleTime = u //strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
}
}
//SetMessageReliability sets the host of the Conf's SAM bridge
func SetMessageReliability(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().MessageReliability = s
return nil
}
}
//SetSAMHost sets the host of the Conf's SAM bridge
func SetSAMHost(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().SamHost = s
return nil
}
}
//SetSAMPort sets the port of the Conf's SAM bridge using a string
func SetSAMPort(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
port, err := strconv.Atoi(s)
if err != nil {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.Config().SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
}
}
//SetSigType sets the type of the forwarder server
func SetSigType(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
switch s {
case "DSA_SHA1":
c.Config().SigType = "DSA_SHA1"
case "ECDSA_SHA256_P256":
c.Config().SigType = "ECDSA_SHA256_P256"
case "ECDSA_SHA384_P384":
c.Config().SigType = "ECDSA_SHA384_P384"
case "ECDSA_SHA512_P521":
c.Config().SigType = "ECDSA_SHA512_P521"
case "EdDSA_SHA512_Ed25519":
c.Config().SigType = "EdDSA_SHA512_Ed25519"
default:
c.Config().SigType = "EdDSA_SHA512_Ed25519"
}
return nil
}
}
//SetType sets the type of the forwarder server
func SetType(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
switch c.Config().Type {
case "server":
c.Config().Type = s
case "http":
c.Config().Type = s
case "client":
c.Config().Type = s
case "httpclient":
c.Config().Type = s
case "browserclient":
c.Config().Type = s
case "udpserver":
c.Config().Type = s
case "udpclient":
c.Config().Type = s
case "vpnserver":
c.Config().Type = s
case "vpnclient":
c.Config().Type = s
case "kcpclient":
c.Config().Type = s
case "kcpserver":
c.Config().Type = s
default:
c.Config().Type = "browserclient"
}
return nil
}
}
//SetUserName sets the host of the Conf's SAM bridge
func SetUserName(s string) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().UserName = s
return nil
}
}
//SetInVariance sets the variance of a number of hops inbound
func SetInVariance(i int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if i < 7 && i > -7 {
c.Config().InVariance = i //strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
}
}
//SetOutVariance sets the variance of a number of hops outbound
func SetOutVariance(i int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if i < 7 && i > -7 {
c.Config().OutVariance = i //strconv.Itoa(i)
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
}
}
//SetAllowZeroIn tells the tunnel to accept zero-hop peers
func SetAllowZeroIn(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().InAllowZeroHop = b // "false"
return nil
}
}
//SetAllowZeroOut tells the tunnel to accept zero-hop peers
func SetAllowZeroOut(b bool) func(SAMTunnel) error {
return func(c SAMTunnel) error {
c.Config().OutAllowZeroHop = b // "false"
return nil
}
}
//SetInBackups sets the inbound tunnel backups
func SetInBackups(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u < 6 && u >= 0 {
c.Config().InBackupQuantity = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
}
}
//SetOutBackups sets the inbound tunnel backups
func SetOutBackups(u int) func(SAMTunnel) error {
return func(c SAMTunnel) error {
if u < 6 && u >= 0 {
c.Config().OutBackupQuantity = u // strconv.Itoa(u)
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
}
}

View File

@@ -1,11 +1,12 @@
package samtunnel
import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam3/i2pkeys"
)
type SAMTunnel interface {
Config() *i2ptunconf.Conf
// Tunnel Options
GetType() string // Get the type of the tunnel in use(server, client, http, udp, etc)
Print() string // Print all the tunnel options as a string

View File

@@ -13,15 +13,7 @@ type ManagerOption func(*SAMManager) error
//SetManagerFilePath sets the host of the SAMManager's SAM bridge
func SetManagerFilePath(s string) func(*SAMManager) error {
return func(c *SAMManager) error {
c.FilePath = s
return nil
}
}
//SetManagerSaveFile tells the router to use an encrypted leaseset
func SetManagerSaveFile(b bool) func(*SAMManager) error {
return func(c *SAMManager) error {
c.save = b
c.config.FilePath = s
return nil
}
}

View File

@@ -12,15 +12,14 @@ import (
import (
"github.com/eyedeekay/portcheck"
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/config/helpers"
"github.com/eyedeekay/sam-forwarder/handler"
"github.com/justinas/nosurf"
)
type SAMManager struct {
FilePath string
save bool
start bool
config *i2ptunconf.Conf
start bool
config *i2ptunconf.Conf
tunName string
@@ -89,11 +88,10 @@ var runningUser = User()
func NewSAMManagerFromOptions(opts ...func(*SAMManager) error) (*SAMManager, error) {
var s SAMManager
s.FilePath = ""
s.save = true
s.config = i2ptunconf.NewI2PBlankTunConf()
s.config.FilePath = ""
s.start = false
s.UseWeb = true
s.config = i2ptunconf.NewI2PBlankTunConf()
s.ServerHost = "localhost"
s.ServerPort = "8081"
s.SamHost = "localhost"
@@ -110,6 +108,7 @@ func NewSAMManagerFromOptions(opts ...func(*SAMManager) error) (*SAMManager, err
return nil, err
}
}
log.Println("MANAGER INITIALIZING")
if port, err := strconv.Atoi(s.WebPort); err != nil {
log.Println("Error:", err)
return nil, err
@@ -121,77 +120,79 @@ func NewSAMManagerFromOptions(opts ...func(*SAMManager) error) (*SAMManager, err
}
}
s.handlerMux = samtunnelhandler.NewTunnelHandlerMux(s.WebHost, s.WebPort, s.config.UserName, s.config.Password, s.cssFile, s.jsFile)
log.Println("tunnel settings", s.ServerHost, s.ServerPort, s.SamHost, s.SamPort)
log.Println("tunnel settings from", s.config.FilePath, "are", s.ServerHost, s.ServerPort, s.SamHost, s.SamPort)
var err error
if s.FilePath != "" {
s.config, err = i2ptunconf.NewI2PTunConf(s.FilePath)
if s.config.FilePath != "" {
s.config, err = i2ptunconf.NewI2PTunConf(s.config.FilePath)
s.config.TargetHost = s.config.GetHost(s.ServerHost, "127.0.0.1")
s.config.TargetPort = s.config.GetPort(s.ServerPort, "8081")
if err != nil {
return nil, err
}
log.Println("Manager found Labels", s.config.Labels)
for _, label := range s.config.Labels {
log.Println("Processing tunnel for label", label)
if t, e := s.config.Get("type", label); e {
switch t {
case "http":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found http under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "httpclient":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMHTTPClientFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMHTTPClientFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found http under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "server":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found server under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "client":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMClientForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMClientForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found client under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "udpserver":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMSSUForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMSSUForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found udpserver under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "udpclient":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMSSUClientForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMSSUClientForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found udpclient under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
/*case "vpnserver":
if f, e := samtunnelhandler.NewTunnelHandler(samforwardervpnserver.NewSAMVPNForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(samforwardervpnserver.NewSAMVPNForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found vpnserver under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "vpnclient":
if f, e := samtunnelhandler.NewTunnelHandler(samforwardervpn.NewSAMVPNClientForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(samforwardervpn.NewSAMVPNClientForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found vpnclient under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}*/
default:
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMForwarderFromConfig(s.FilePath, s.SamHost, s.SamPort, label)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMForwarderFromConfig(s.config.FilePath, s.SamHost, s.SamPort, label)); e == nil {
log.Println("found server under", label)
s.handlerMux = s.handlerMux.Append(f)
} else {
@@ -208,49 +209,49 @@ func NewSAMManagerFromOptions(opts ...func(*SAMManager) error) (*SAMManager, err
}
switch t {
case "http":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMForwarderFromConf(s.config)); e == nil {
log.Println("found default http")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "httpclient":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMHTTPClientFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMHTTPClientFromConf(s.config)); e == nil {
log.Println("found default httpclient")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "browserclient":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMBrowserClientFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMBrowserClientFromConf(s.config)); e == nil {
log.Println("found default browserclient")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "server":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMForwarderFromConf(s.config)); e == nil {
log.Println("found default server")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "client":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMClientForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMClientForwarderFromConf(s.config)); e == nil {
log.Println("found default client")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "udpserver":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMSSUForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMSSUForwarderFromConf(s.config)); e == nil {
log.Println("found default udpserver")
s.handlerMux = s.handlerMux.Append(f)
} else {
return nil, e
}
case "udpclient":
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMSSUClientForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMSSUClientForwarderFromConf(s.config)); e == nil {
log.Println("found default udpclient")
s.handlerMux = s.handlerMux.Append(f)
} else {
@@ -271,7 +272,7 @@ func NewSAMManagerFromOptions(opts ...func(*SAMManager) error) (*SAMManager, err
return nil, e
}*/
default:
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunconf.NewSAMClientForwarderFromConf(s.config)); e == nil {
if f, e := samtunnelhandler.NewTunnelHandler(i2ptunhelper.NewSAMClientForwarderFromConf(s.config)); e == nil {
log.Println("found default client")
s.handlerMux = s.handlerMux.Append(f)
} else {

View File

@@ -15,7 +15,6 @@ import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/manager"
//"github.com/eyedeekay/samcatd-web"
)
type flagOpts []string
@@ -191,6 +190,7 @@ func lbMain(ctx context.Context) {
}
config.TargetHost = config.GetHost(*targetHost, "127.0.0.1")
config.TargetPort = config.GetPort(*targetPort, "8081")
config.FilePath = *iniFile
config.SaveFile = config.GetSaveFile(*saveFile, true)
config.SaveDirectory = config.GetDir(*targetDir, "../")
config.SamHost = config.GetSAMHost(*samHost, "127.0.0.1")
@@ -218,7 +218,7 @@ func lbMain(ctx context.Context) {
config.AccessListType = config.GetAccessListType(*accessListType, "none")
config.CloseIdle = config.GetCloseOnIdle(*closeIdle, false)
config.CloseIdleTime = config.GetCloseIdleTime(*closeIdleTime, 600000)
config.Type = config.GetType(*client, *udpMode, *injectHeaders, "server")
config.Type = config.GetTypes(*client, *udpMode, *injectHeaders, "server")
config.TargetForPort443 = config.GetPort443(*targetPort443, "")
config.KeyFilePath = config.GetKeyFile(*encryptKeyFiles, "")
config.ClientDest = config.GetClientDest(*targetDest, "", "")

View File

@@ -11,7 +11,7 @@ type ClientOption func(*SAMClientForwarder) error
//SetClientFilePath sets the host of the SAMClientForwarder's SAM bridge
func SetClientFilePath(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.FilePath = s
c.Conf.FilePath = s
return nil
}
}
@@ -19,7 +19,7 @@ func SetClientFilePath(s string) func(*SAMClientForwarder) error {
//SetClientSaveFile tells the router to save the tunnel keys long-term
func SetClientSaveFile(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.save = b
c.Conf.SaveFile = b
return nil
}
}
@@ -27,7 +27,7 @@ func SetClientSaveFile(b bool) func(*SAMClientForwarder) error {
//SetClientHost sets the host of the SAMClientForwarder's SAM bridge
func SetClientHost(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.TargetHost = s
c.Conf.TargetHost = s
return nil
}
}
@@ -35,7 +35,7 @@ func SetClientHost(s string) func(*SAMClientForwarder) error {
//SetClientDestination sets the destination to forwarder SAMClientForwarder's to
func SetClientDestination(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.dest = s
c.Conf.ClientDest = s
return nil
}
}
@@ -48,7 +48,7 @@ func SetClientPort(s string) func(*SAMClientForwarder) error {
return fmt.Errorf("Invalid TCP Client Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetPort = s
c.Conf.TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -58,7 +58,7 @@ func SetClientPort(s string) func(*SAMClientForwarder) error {
//SetClientSAMHost sets the host of the SAMClientForwarder's SAM bridge
func SetClientSAMHost(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -71,7 +71,7 @@ func SetClientSAMPort(s string) func(*SAMClientForwarder) error {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -81,7 +81,7 @@ func SetClientSAMPort(s string) func(*SAMClientForwarder) error {
//SetClientName sets the host of the SAMClientForwarder's SAM bridge
func SetClientName(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.TunName = s
c.Conf.TunName = s
return nil
}
}
@@ -90,19 +90,19 @@ func SetClientName(s string) func(*SAMClientForwarder) error {
func SetClientSigType(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if s == "" {
c.sigType = ""
c.Conf.SigType = ""
} else if s == "DSA_SHA1" {
c.sigType = "DSA_SHA1"
c.Conf.SigType = "DSA_SHA1"
} else if s == "ECDSA_SHA256_P256" {
c.sigType = "ECDSA_SHA256_P256"
c.Conf.SigType = "ECDSA_SHA256_P256"
} else if s == "ECDSA_SHA384_P384" {
c.sigType = "ECDSA_SHA384_P384"
c.Conf.SigType = "ECDSA_SHA384_P384"
} else if s == "ECDSA_SHA512_P521" {
c.sigType = "ECDSA_SHA512_P521"
c.Conf.SigType = "ECDSA_SHA512_P521"
} else if s == "EdDSA_SHA512_Ed25519" {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
} else {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
}
return nil
}
@@ -112,7 +112,7 @@ func SetClientSigType(s string) func(*SAMClientForwarder) error {
func SetClientInLength(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u < 7 && u >= 0 {
c.inLength = strconv.Itoa(u)
c.Conf.InLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -123,7 +123,7 @@ func SetClientInLength(u int) func(*SAMClientForwarder) error {
func SetClientOutLength(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u < 7 && u >= 0 {
c.outLength = strconv.Itoa(u)
c.Conf.OutLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -134,7 +134,7 @@ func SetClientOutLength(u int) func(*SAMClientForwarder) error {
func SetClientInVariance(i int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if i < 7 && i > -7 {
c.inVariance = strconv.Itoa(i)
c.Conf.InVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -145,7 +145,7 @@ func SetClientInVariance(i int) func(*SAMClientForwarder) error {
func SetClientOutVariance(i int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if i < 7 && i > -7 {
c.outVariance = strconv.Itoa(i)
c.Conf.OutVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -156,7 +156,7 @@ func SetClientOutVariance(i int) func(*SAMClientForwarder) error {
func SetClientInQuantity(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u <= 16 && u > 0 {
c.inQuantity = strconv.Itoa(u)
c.Conf.InQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -167,7 +167,7 @@ func SetClientInQuantity(u int) func(*SAMClientForwarder) error {
func SetClientOutQuantity(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u <= 16 && u > 0 {
c.outQuantity = strconv.Itoa(u)
c.Conf.OutQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -178,7 +178,7 @@ func SetClientOutQuantity(u int) func(*SAMClientForwarder) error {
func SetClientInBackups(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u < 6 && u >= 0 {
c.inBackupQuantity = strconv.Itoa(u)
c.Conf.InBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -189,7 +189,7 @@ func SetClientInBackups(u int) func(*SAMClientForwarder) error {
func SetClientOutBackups(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u < 6 && u >= 0 {
c.outBackupQuantity = strconv.Itoa(u)
c.Conf.OutBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -200,10 +200,10 @@ func SetClientOutBackups(u int) func(*SAMClientForwarder) error {
func SetClientEncrypt(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.encryptLeaseSet = "true"
c.Conf.EncryptLeaseSet = true
return nil
}
c.encryptLeaseSet = "false"
c.Conf.EncryptLeaseSet = false
return nil
}
}
@@ -211,7 +211,7 @@ func SetClientEncrypt(b bool) func(*SAMClientForwarder) error {
//SetClientLeaseSetKey sets
func SetClientLeaseSetKey(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.leaseSetKey = s
c.Conf.LeaseSetKey = s
return nil
}
}
@@ -219,7 +219,7 @@ func SetClientLeaseSetKey(s string) func(*SAMClientForwarder) error {
//SetClientLeaseSetPrivateKey sets
func SetClientLeaseSetPrivateKey(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.leaseSetPrivateKey = s
c.Conf.LeaseSetPrivateKey = s
return nil
}
}
@@ -227,7 +227,7 @@ func SetClientLeaseSetPrivateKey(s string) func(*SAMClientForwarder) error {
//SetClientLeaseSetPrivateSigningKey sets
func SetClientLeaseSetPrivateSigningKey(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.leaseSetPrivateSigningKey = s
c.Conf.LeaseSetPrivateSigningKey = s
return nil
}
}
@@ -235,7 +235,7 @@ func SetClientLeaseSetPrivateSigningKey(s string) func(*SAMClientForwarder) erro
//SetClientMessageReliability sets
func SetClientMessageReliability(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.messageReliability = s
c.Conf.MessageReliability = s
return nil
}
}
@@ -244,10 +244,10 @@ func SetClientMessageReliability(s string) func(*SAMClientForwarder) error {
func SetClientAllowZeroIn(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.inAllowZeroHop = "true"
c.Conf.InAllowZeroHop = true
return nil
}
c.inAllowZeroHop = "false"
c.Conf.InAllowZeroHop = false
return nil
}
}
@@ -256,10 +256,10 @@ func SetClientAllowZeroIn(b bool) func(*SAMClientForwarder) error {
func SetClientAllowZeroOut(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.outAllowZeroHop = "true"
c.Conf.OutAllowZeroHop = true
return nil
}
c.outAllowZeroHop = "false"
c.Conf.OutAllowZeroHop = false
return nil
}
}
@@ -268,10 +268,10 @@ func SetClientAllowZeroOut(b bool) func(*SAMClientForwarder) error {
func SetClientFastRecieve(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.fastRecieve = "true"
c.Conf.FastRecieve = true
return nil
}
c.fastRecieve = "false"
c.Conf.FastRecieve = false
return nil
}
}
@@ -280,10 +280,10 @@ func SetClientFastRecieve(b bool) func(*SAMClientForwarder) error {
func SetClientCompress(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.useCompression = "true"
c.Conf.UseCompression = true
return nil
}
c.useCompression = "false"
c.Conf.UseCompression = false
return nil
}
}
@@ -292,10 +292,10 @@ func SetClientCompress(b bool) func(*SAMClientForwarder) error {
func SetClientReduceIdle(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.reduceIdle = "true"
c.Conf.ReduceIdle = true
return nil
}
c.reduceIdle = "false"
c.Conf.ReduceIdle = false
return nil
}
}
@@ -303,9 +303,9 @@ func SetClientReduceIdle(b bool) func(*SAMClientForwarder) error {
//SetClientReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetClientReduceIdleTime(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.reduceIdleTime = strconv.Itoa(300000)
c.Conf.ReduceIdleTime = 300000
if u >= 6 {
c.reduceIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.ReduceIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u)
@@ -315,9 +315,9 @@ func SetClientReduceIdleTime(u int) func(*SAMClientForwarder) error {
//SetClientReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetClientReduceIdleTimeMs(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.reduceIdleTime = strconv.Itoa(300000)
c.Conf.ReduceIdleTime = 300000
if u >= 300000 {
c.reduceIdleTime = strconv.Itoa(u)
c.Conf.ReduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
@@ -328,7 +328,7 @@ func SetClientReduceIdleTimeMs(u int) func(*SAMClientForwarder) error {
func SetClientReduceIdleQuantity(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if u < 5 {
c.reduceIdleQuantity = strconv.Itoa(u)
c.Conf.ReduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
@@ -339,10 +339,10 @@ func SetClientReduceIdleQuantity(u int) func(*SAMClientForwarder) error {
func SetClientCloseIdle(b bool) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if b {
c.closeIdle = "true"
c.Conf.CloseIdle = true
return nil
}
c.closeIdle = "false"
c.Conf.CloseIdle = false
return nil
}
}
@@ -350,9 +350,9 @@ func SetClientCloseIdle(b bool) func(*SAMClientForwarder) error {
//SetClientCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetClientCloseIdleTime(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 6 {
c.closeIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.CloseIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
@@ -362,9 +362,9 @@ func SetClientCloseIdleTime(u int) func(*SAMClientForwarder) error {
//SetClientCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetClientCloseIdleTimeMs(u int) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 300000 {
c.closeIdleTime = strconv.Itoa(u)
c.Conf.CloseIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
@@ -375,16 +375,16 @@ func SetClientCloseIdleTimeMs(u int) func(*SAMClientForwarder) error {
func SetClientAccessListType(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if s == "whitelist" {
c.accessListType = "whitelist"
c.Conf.AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.accessListType = "blacklist"
c.Conf.AccessListType = "blacklist"
return nil
} else if s == "none" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
} else if s == "" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
@@ -396,7 +396,7 @@ func SetClientAccessList(s []string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
if len(s) > 0 {
for _, a := range s {
c.accessList = append(c.accessList, a)
c.Conf.AccessList = append(c.Conf.AccessList, a)
}
return nil
}
@@ -407,7 +407,7 @@ func SetClientAccessList(s []string) func(*SAMClientForwarder) error {
//SetKeyFile sets
func SetClientPassword(s string) func(*SAMClientForwarder) error {
return func(c *SAMClientForwarder) error {
c.passfile = s
c.Conf.KeyFilePath = s
return nil
}
}

View File

@@ -11,6 +11,7 @@ import (
)
import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam-forwarder/interface"
@@ -20,67 +21,30 @@ import (
// SAMClientForwarder is a tcp proxy that automatically forwards ports to i2p
type SAMClientForwarder struct {
SamHost string
SamPort string
TunName string
Type string
TargetHost string
TargetPort string
samConn *sam3.SAM
SamKeys i2pkeys.I2PKeys
Hasher *hashhash.Hasher
connectStream *sam3.StreamSession
dest string
addr i2pkeys.I2PAddr
publishConnection net.Listener
FilePath string
file io.ReadWriter
save bool
up bool
file io.ReadWriter
up bool
// samcatd options
passfile string
sigType string
// config
Conf *i2ptunconf.Conf
}
// I2CP options
encryptLeaseSet string
leaseSetKey string
leaseSetPrivateKey string
leaseSetPrivateSigningKey string
LeaseSetKeys *i2pkeys.I2PKeys
inAllowZeroHop string
outAllowZeroHop string
inLength string
outLength string
inQuantity string
outQuantity string
inVariance string
outVariance string
inBackupQuantity string
outBackupQuantity string
fastRecieve string
useCompression string
messageReliability string
closeIdle string
closeIdleTime string
reduceIdle string
reduceIdleTime string
reduceIdleQuantity string
//Streaming Library options
accessListType string
accessList []string
func (f *SAMClientForwarder) Config() *i2ptunconf.Conf {
return f.Conf
}
func (f *SAMClientForwarder) GetType() string {
return f.Type
return f.Config().Type
}
func (f *SAMClientForwarder) ID() string {
return f.TunName
return f.Config().TunName
}
func (f *SAMClientForwarder) Keys() i2pkeys.I2PKeys {
@@ -88,32 +52,7 @@ func (f *SAMClientForwarder) Keys() i2pkeys.I2PKeys {
}
func (f *SAMClientForwarder) print() []string {
lsk, lspk, lspsk := f.leasesetsettings()
return []string{
//f.targetForPort443(),
"inbound.length=" + f.inLength,
"outbound.length=" + f.outLength,
"inbound.lengthVariance=" + f.inVariance,
"outbound.lengthVariance=" + f.outVariance,
"inbound.backupQuantity=" + f.inBackupQuantity,
"outbound.backupQuantity=" + f.outBackupQuantity,
"inbound.quantity=" + f.inQuantity,
"outbound.quantity=" + f.outQuantity,
"inbound.allowZeroHop=" + f.inAllowZeroHop,
"outbound.allowZeroHop=" + f.outAllowZeroHop,
"i2cp.fastRecieve=" + f.fastRecieve,
"i2cp.gzip=" + f.useCompression,
"i2cp.reduceOnIdle=" + f.reduceIdle,
"i2cp.reduceIdleTime=" + f.reduceIdleTime,
"i2cp.reduceQuantity=" + f.reduceIdleQuantity,
"i2cp.closeOnIdle=" + f.closeIdle,
"i2cp.closeIdleTime=" + f.closeIdleTime,
"i2cp.messageReliability=" + f.messageReliability,
"i2cp.encryptLeaseSet=" + f.encryptLeaseSet,
lsk, lspk, lspsk,
f.accesslisttype(),
f.accesslist(),
}
return f.Config().PrintSlice()
}
func (f *SAMClientForwarder) Props() map[string]string {
@@ -137,12 +76,12 @@ func (f *SAMClientForwarder) Cleanup() {
func (f *SAMClientForwarder) Print() string {
var r string
r += "name=" + f.TunName + "\n"
r += "type=" + f.Type + "\n"
r += "name=" + f.Config().TunName + "\n"
r += "type=" + f.Config().Type + "\n"
r += "base32=" + f.Base32() + "\n"
r += "base64=" + f.Base64() + "\n"
r += "dest=" + f.dest + "\n"
r += "ntcpclient\n"
r += "dest=" + f.Config().ClientDest + "\n"
//r += "ntcpclient\n"
for _, s := range f.print() {
r += s + "\n"
}
@@ -164,20 +103,20 @@ func (f *SAMClientForwarder) Search(search string) string {
}
func (f *SAMClientForwarder) accesslisttype() string {
if f.accessListType == "whitelist" {
if f.Config().AccessListType == "whitelist" {
return "i2cp.enableAccessList=true"
} else if f.accessListType == "blacklist" {
} else if f.Config().AccessListType == "blacklist" {
return "i2cp.enableBlackList=true"
} else if f.accessListType == "none" {
} else if f.Config().AccessListType == "none" {
return ""
}
return ""
}
func (f *SAMClientForwarder) accesslist() string {
if f.accessListType != "" && len(f.accessList) > 0 {
if f.Config().AccessListType != "" && len(f.Config().AccessList) > 0 {
r := ""
for _, s := range f.accessList {
for _, s := range f.Config().AccessList {
r += s + ","
}
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
@@ -187,21 +126,21 @@ func (f *SAMClientForwarder) accesslist() string {
func (f *SAMClientForwarder) leasesetsettings() (string, string, string) {
var r, s, t string
if f.leaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.leaseSetKey
if f.Config().LeaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.Config().LeaseSetKey
}
if f.leaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.leaseSetPrivateKey
if f.Config().LeaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.Config().LeaseSetPrivateKey
}
if f.leaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.leaseSetPrivateSigningKey
if f.Config().LeaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.Config().LeaseSetPrivateSigningKey
}
return r, s, t
}
// Target returns the host:port of the local service you want to forward to i2p
func (f *SAMClientForwarder) Target() string {
return f.TargetHost + ":" + f.TargetPort
return f.Config().TargetHost + ":" + f.Config().TargetPort
}
// Destination returns the destination of the i2p service you want to forward locally
@@ -210,7 +149,7 @@ func (f *SAMClientForwarder) Destination() string {
}
func (f *SAMClientForwarder) sam() string {
return f.SamHost + ":" + f.SamPort
return f.Config().SamHost + ":" + f.Config().SamPort
}
//Base32 returns the base32 address of the local destination
@@ -250,10 +189,10 @@ func (f *SAMClientForwarder) forward(conn net.Conn) {
//Serve starts the SAM connection and and forwards the local host:port to i2p
func (f *SAMClientForwarder) Serve() error {
if f.addr, err = f.samConn.Lookup(f.dest); err != nil {
if f.addr, err = f.samConn.Lookup(f.Config().ClientDest); err != nil {
return err
}
if f.connectStream, err = f.samConn.NewStreamSession(f.TunName, f.SamKeys, f.print()); err != nil {
if f.connectStream, err = f.samConn.NewStreamSession(f.Config().TunName, f.SamKeys, f.print()); err != nil {
log.Println("Stream Creation error:", err.Error())
return err
}
@@ -285,25 +224,25 @@ func (f *SAMClientForwarder) Close() error {
}
func (s *SAMClientForwarder) Load() (samtunnel.SAMTunnel, error) {
if s.publishConnection, err = net.Listen("tcp", s.TargetHost+":"+s.TargetPort); err != nil {
if s.publishConnection, err = net.Listen("tcp", s.Config().TargetHost+":"+s.Config().TargetPort); err != nil {
return nil, err
}
if s.samConn, err = sam3.NewSAM(s.sam()); err != nil {
return nil, err
}
log.Println("SAM Bridge connection established.")
if s.save {
if s.Config().SaveFile {
log.Println("Saving i2p keys")
}
if s.SamKeys, err = sfi2pkeys.Load(s.FilePath, s.TunName, s.passfile, s.samConn, s.save); err != nil {
if s.SamKeys, err = sfi2pkeys.Load(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.samConn, s.Config().SaveFile); err != nil {
return nil, err
}
log.Println("Destination keys generated, tunnel name:", s.TunName)
if s.save {
if err := sfi2pkeys.Save(s.FilePath, s.TunName, s.passfile, s.SamKeys); err != nil {
log.Println("Destination keys generated, tunnel name:", s.Config().TunName)
if s.Config().SaveFile {
if err := sfi2pkeys.Save(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.SamKeys); err != nil {
return nil, err
}
log.Println("Saved tunnel keys for", s.TunName)
log.Println("Saved tunnel keys for", s.Config().TunName)
}
s.Hasher, err = hashhash.NewHasher(len(strings.Replace(s.Base32(), ".b32.i2p", "", 1)))
if err != nil {
@@ -321,39 +260,8 @@ func NewSAMClientForwarder(host, port string) (*SAMClientForwarder, error) {
//NewSAMClientForwarderFromOptions makes a new SAM forwarder with default options, accepts host:port arguments
func NewSAMClientForwarderFromOptions(opts ...func(*SAMClientForwarder) error) (*SAMClientForwarder, error) {
var s SAMClientForwarder
s.SamHost = "127.0.0.1"
s.SamPort = "7656"
s.FilePath = ""
s.save = false
s.TargetHost = "127.0.0.1"
s.TargetPort = "0"
s.TunName = "samForwarder"
s.inLength = "3"
s.outLength = "3"
s.inQuantity = "2"
s.outQuantity = "2"
s.inVariance = "1"
s.outVariance = "1"
s.inBackupQuantity = "3"
s.outBackupQuantity = "3"
s.inAllowZeroHop = "false"
s.outAllowZeroHop = "false"
s.encryptLeaseSet = "false"
s.leaseSetKey = ""
s.leaseSetPrivateKey = ""
s.leaseSetPrivateSigningKey = ""
s.fastRecieve = "false"
s.useCompression = "true"
s.reduceIdle = "false"
s.reduceIdleTime = "300000"
s.reduceIdleQuantity = "4"
s.closeIdle = "false"
s.closeIdleTime = "300000"
s.dest = "none"
s.Type = "client"
s.messageReliability = "none"
s.passfile = ""
s.dest = "i2p-projekt.i2p"
s.Conf = i2ptunconf.NewI2PBlankTunConf()
s.Conf.Type = "tcpclient"
for _, o := range opts {
if err := o(&s); err != nil {
return nil, err

View File

@@ -11,7 +11,7 @@ type Option func(*SAMForwarder) error
//SetFilePath sets the path to save the config file at.
func SetFilePath(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.FilePath = s
c.Conf.FilePath = s
return nil
}
}
@@ -20,10 +20,10 @@ func SetFilePath(s string) func(*SAMForwarder) error {
func SetType(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if s == "http" {
c.Type = s
c.Conf.Type = s
return nil
} else {
c.Type = "server"
c.Conf.Type = "server"
return nil
}
}
@@ -33,19 +33,19 @@ func SetType(s string) func(*SAMForwarder) error {
func SetSigType(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if s == "" {
c.sigType = ""
c.Conf.SigType = ""
} else if s == "DSA_SHA1" {
c.sigType = "DSA_SHA1"
c.Conf.SigType = "DSA_SHA1"
} else if s == "ECDSA_SHA256_P256" {
c.sigType = "ECDSA_SHA256_P256"
c.Conf.SigType = "ECDSA_SHA256_P256"
} else if s == "ECDSA_SHA384_P384" {
c.sigType = "ECDSA_SHA384_P384"
c.Conf.SigType = "ECDSA_SHA384_P384"
} else if s == "ECDSA_SHA512_P521" {
c.sigType = "ECDSA_SHA512_P521"
c.Conf.SigType = "ECDSA_SHA512_P521"
} else if s == "EdDSA_SHA512_Ed25519" {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
} else {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
}
return nil
}
@@ -54,7 +54,7 @@ func SetSigType(s string) func(*SAMForwarder) error {
//SetSaveFile tells the router to save the tunnel's keys long-term
func SetSaveFile(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.save = b
c.Conf.SaveFile = b
return nil
}
}
@@ -62,7 +62,7 @@ func SetSaveFile(b bool) func(*SAMForwarder) error {
//SetHost sets the host of the service to forward
func SetHost(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.TargetHost = s
c.Conf.TargetHost = s
return nil
}
}
@@ -75,7 +75,7 @@ func SetPort(s string) func(*SAMForwarder) error {
return fmt.Errorf("Invalid TCP Server Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetPort = s
c.Conf.TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -85,7 +85,7 @@ func SetPort(s string) func(*SAMForwarder) error {
//SetSAMHost sets the host of the SAMForwarder's SAM bridge
func SetSAMHost(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -98,7 +98,7 @@ func SetSAMPort(s string) func(*SAMForwarder) error {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -108,7 +108,7 @@ func SetSAMPort(s string) func(*SAMForwarder) error {
//SetName sets the host of the SAMForwarder's SAM bridge
func SetName(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.TunName = s
c.Conf.TunName = s
return nil
}
}
@@ -117,7 +117,7 @@ func SetName(s string) func(*SAMForwarder) error {
func SetInLength(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u < 7 && u >= 0 {
c.inLength = strconv.Itoa(u)
c.Conf.InLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -128,7 +128,7 @@ func SetInLength(u int) func(*SAMForwarder) error {
func SetOutLength(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u < 7 && u >= 0 {
c.outLength = strconv.Itoa(u)
c.Conf.OutLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -139,7 +139,7 @@ func SetOutLength(u int) func(*SAMForwarder) error {
func SetInVariance(i int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if i < 7 && i > -7 {
c.inVariance = strconv.Itoa(i)
c.Conf.InVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -150,7 +150,7 @@ func SetInVariance(i int) func(*SAMForwarder) error {
func SetOutVariance(i int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if i < 7 && i > -7 {
c.outVariance = strconv.Itoa(i)
c.Conf.OutVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -161,7 +161,7 @@ func SetOutVariance(i int) func(*SAMForwarder) error {
func SetInQuantity(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u <= 16 && u > 0 {
c.inQuantity = strconv.Itoa(u)
c.Conf.InQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -172,7 +172,7 @@ func SetInQuantity(u int) func(*SAMForwarder) error {
func SetOutQuantity(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u <= 16 && u > 0 {
c.outQuantity = strconv.Itoa(u)
c.Conf.OutQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -183,7 +183,7 @@ func SetOutQuantity(u int) func(*SAMForwarder) error {
func SetInBackups(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u < 6 && u >= 0 {
c.inBackupQuantity = strconv.Itoa(u)
c.Conf.InBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -194,7 +194,7 @@ func SetInBackups(u int) func(*SAMForwarder) error {
func SetOutBackups(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u < 6 && u >= 0 {
c.outBackupQuantity = strconv.Itoa(u)
c.Conf.OutBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -205,10 +205,10 @@ func SetOutBackups(u int) func(*SAMForwarder) error {
func SetEncrypt(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.encryptLeaseSet = "true"
c.Conf.EncryptLeaseSet = true
return nil
}
c.encryptLeaseSet = "false"
c.Conf.EncryptLeaseSet = false
return nil
}
}
@@ -216,7 +216,7 @@ func SetEncrypt(b bool) func(*SAMForwarder) error {
//SetLeaseSetKey sets the host of the SAMForwarder's SAM bridge
func SetLeaseSetKey(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.leaseSetKey = s
c.Conf.LeaseSetKey = s
return nil
}
}
@@ -224,7 +224,7 @@ func SetLeaseSetKey(s string) func(*SAMForwarder) error {
//SetLeaseSetPrivateKey sets the host of the SAMForwarder's SAM bridge
func SetLeaseSetPrivateKey(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.leaseSetPrivateKey = s
c.Conf.LeaseSetPrivateKey = s
return nil
}
}
@@ -232,7 +232,7 @@ func SetLeaseSetPrivateKey(s string) func(*SAMForwarder) error {
//SetLeaseSetPrivateSigningKey sets the host of the SAMForwarder's SAM bridge
func SetLeaseSetPrivateSigningKey(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.leaseSetPrivateSigningKey = s
c.Conf.LeaseSetPrivateSigningKey = s
return nil
}
}
@@ -240,7 +240,7 @@ func SetLeaseSetPrivateSigningKey(s string) func(*SAMForwarder) error {
//SetMessageReliability sets the host of the SAMForwarder's SAM bridge
func SetMessageReliability(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.messageReliability = s
c.Conf.MessageReliability = s
return nil
}
}
@@ -249,10 +249,10 @@ func SetMessageReliability(s string) func(*SAMForwarder) error {
func SetAllowZeroIn(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.inAllowZeroHop = "true"
c.Conf.InAllowZeroHop = true
return nil
}
c.inAllowZeroHop = "false"
c.Conf.InAllowZeroHop = false
return nil
}
}
@@ -261,10 +261,10 @@ func SetAllowZeroIn(b bool) func(*SAMForwarder) error {
func SetAllowZeroOut(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.outAllowZeroHop = "true"
c.Conf.OutAllowZeroHop = true
return nil
}
c.outAllowZeroHop = "false"
c.Conf.OutAllowZeroHop = false
return nil
}
}
@@ -273,10 +273,10 @@ func SetAllowZeroOut(b bool) func(*SAMForwarder) error {
func SetCompress(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.useCompression = "true"
c.Conf.UseCompression = true
return nil
}
c.useCompression = "false"
c.Conf.UseCompression = false
return nil
}
}
@@ -285,10 +285,10 @@ func SetCompress(b bool) func(*SAMForwarder) error {
func SetFastRecieve(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.fastRecieve = "true"
c.Conf.FastRecieve = true
return nil
}
c.fastRecieve = "false"
c.Conf.FastRecieve = false
return nil
}
}
@@ -297,10 +297,10 @@ func SetFastRecieve(b bool) func(*SAMForwarder) error {
func SetReduceIdle(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.reduceIdle = "true"
c.Conf.ReduceIdle = true
return nil
}
c.reduceIdle = "false"
c.Conf.ReduceIdle = false
return nil
}
}
@@ -308,9 +308,9 @@ func SetReduceIdle(b bool) func(*SAMForwarder) error {
//SetReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetReduceIdleTime(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 6 {
c.reduceIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.ReduceIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes) %v", u)
@@ -320,9 +320,9 @@ func SetReduceIdleTime(u int) func(*SAMForwarder) error {
//SetReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetReduceIdleTimeMs(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 300000 {
c.reduceIdleTime = strconv.Itoa(u)
c.Conf.ReduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in milliseconds) %v", u)
@@ -333,7 +333,7 @@ func SetReduceIdleTimeMs(u int) func(*SAMForwarder) error {
func SetReduceIdleQuantity(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if u < 5 {
c.reduceIdleQuantity = strconv.Itoa(u)
c.Conf.ReduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
@@ -344,10 +344,10 @@ func SetReduceIdleQuantity(u int) func(*SAMForwarder) error {
func SetCloseIdle(b bool) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if b {
c.closeIdle = "true"
c.Conf.CloseIdle = true
return nil
}
c.closeIdle = "false"
c.Conf.CloseIdle = false
return nil
}
}
@@ -355,9 +355,9 @@ func SetCloseIdle(b bool) func(*SAMForwarder) error {
//SetCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetCloseIdleTime(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 6 {
c.closeIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.CloseIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
@@ -367,41 +367,41 @@ func SetCloseIdleTime(u int) func(*SAMForwarder) error {
//SetCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetCloseIdleTimeMs(u int) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 300000 {
c.closeIdleTime = strconv.Itoa(u)
c.Conf.CloseIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
}
}
//SetAccessListType tells the system to treat the accessList as a whitelist
//SetAccessListType tells the system to treat the AccessList as a whitelist
func SetAccessListType(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if s == "whitelist" {
c.accessListType = "whitelist"
c.Conf.AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.accessListType = "blacklist"
c.Conf.AccessListType = "blacklist"
return nil
} else if s == "none" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
} else if s == "" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
}
}
//SetAccessList tells the system to treat the accessList as a whitelist
//SetAccessList tells the system to treat the AccessList as a whitelist
func SetAccessList(s []string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
if len(s) > 0 {
for _, a := range s {
c.accessList = append(c.accessList, a)
c.Conf.AccessList = append(c.Conf.AccessList, a)
}
return nil
}
@@ -417,7 +417,7 @@ func SetAccessList(s []string) func(*SAMForwarder) error {
return fmt.Errorf("Invalid Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetForPort443 = s
c.Conf.TargetForPort443 = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -428,7 +428,7 @@ func SetAccessList(s []string) func(*SAMForwarder) error {
//SetKeyFile sets
func SetKeyFile(s string) func(*SAMForwarder) error {
return func(c *SAMForwarder) error {
c.passfile = s
c.Conf.KeyFilePath = s
return nil
}
}

View File

@@ -14,6 +14,7 @@ import (
)
import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam-forwarder/interface"
@@ -24,58 +25,17 @@ import (
//SAMForwarder is a structure which automatically configured the forwarding of
//a local service to i2p over the SAM API.
type SAMForwarder struct {
SamHost string
SamPort string
TunName string
TargetHost string
TargetPort string
samConn *sam3.SAM
SamKeys i2pkeys.I2PKeys
Hasher *hashhash.Hasher
publishStream *sam3.StreamSession
publishListen *sam3.StreamListener
FilePath string
file io.ReadWriter
save bool
up bool
file io.ReadWriter
up bool
Type string
// samcatd options
passfile string
sigType string
// I2CP options
encryptLeaseSet string
leaseSetKey string
leaseSetPrivateKey string
leaseSetPrivateSigningKey string
LeaseSetKeys *i2pkeys.I2PKeys
inAllowZeroHop string
outAllowZeroHop string
inLength string
outLength string
inQuantity string
outQuantity string
inVariance string
outVariance string
inBackupQuantity string
outBackupQuantity string
fastRecieve string
useCompression string
messageReliability string
closeIdle string
closeIdleTime string
reduceIdle string
reduceIdleTime string
reduceIdleQuantity string
//Streaming Library options
accessListType string
accessList []string
// conf
Conf *i2ptunconf.Conf
clientLock bool
connLock bool
@@ -86,8 +46,12 @@ type SAMForwarder struct {
var err error
func (f *SAMForwarder) Config() *i2ptunconf.Conf {
return f.Conf
}
func (f *SAMForwarder) ID() string {
return f.TunName
return f.Config().TunName
}
func (f *SAMForwarder) Keys() i2pkeys.I2PKeys {
@@ -101,7 +65,7 @@ func (f *SAMForwarder) Cleanup() {
}
func (f *SAMForwarder) GetType() string {
return f.Type
return f.Conf.Type
}
/*func (f *SAMForwarder) targetForPort443() string {
@@ -112,7 +76,7 @@ func (f *SAMForwarder) GetType() string {
}*/
func (f *SAMForwarder) print() []string {
lsk, lspk, lspsk := f.leasesetsettings()
/*lsk, lspk, lspsk := f.Config().Leasesetsettings()
return []string{
//f.targetForPort443(),
"inbound.length=" + f.inLength,
@@ -137,7 +101,8 @@ func (f *SAMForwarder) print() []string {
lsk, lspk, lspsk,
f.accesslisttype(),
f.accesslist(),
}
}*/
return f.Conf.PrintSlice()
}
func (f *SAMForwarder) Props() map[string]string {
@@ -155,11 +120,11 @@ func (f *SAMForwarder) Props() map[string]string {
func (f *SAMForwarder) Print() string {
var r string
r += "name=" + f.TunName + "\n"
r += "type=" + f.Type + "\n"
r += "name=" + f.Config().TunName + "\n"
r += "type=" + f.Conf.Type + "\n"
r += "base32=" + f.Base32() + "\n"
r += "base64=" + f.Base64() + "\n"
if f.Type == "http" {
if f.Conf.Type == "http" {
r += "httpserver\n"
} else {
r += "ntcpserver\n"
@@ -184,20 +149,20 @@ func (f *SAMForwarder) Search(search string) string {
}
func (f *SAMForwarder) accesslisttype() string {
if f.accessListType == "whitelist" {
if f.Config().AccessListType == "whitelist" {
return "i2cp.enableAccessList=true"
} else if f.accessListType == "blacklist" {
} else if f.Config().AccessListType == "blacklist" {
return "i2cp.enableBlackList=true"
} else if f.accessListType == "none" {
} else if f.Config().AccessListType == "none" {
return ""
}
return ""
}
func (f *SAMForwarder) accesslist() string {
if f.accessListType != "" && len(f.accessList) > 0 {
if f.Config().AccessListType != "" && len(f.Config().AccessList) > 0 {
r := ""
for _, s := range f.accessList {
for _, s := range f.Config().AccessList {
r += s + ","
}
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
@@ -207,25 +172,25 @@ func (f *SAMForwarder) accesslist() string {
func (f *SAMForwarder) leasesetsettings() (string, string, string) {
var r, s, t string
if f.leaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.leaseSetKey
if f.Config().LeaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.Config().LeaseSetKey
}
if f.leaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.leaseSetPrivateKey
if f.Config().LeaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.Config().LeaseSetPrivateKey
}
if f.leaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.leaseSetPrivateSigningKey
if f.Config().LeaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.Config().LeaseSetPrivateSigningKey
}
return r, s, t
}
// Target returns the host:port of the local service you want to forward to i2p
func (f *SAMForwarder) Target() string {
return f.TargetHost + ":" + f.TargetPort
return f.Config().TargetHost + ":" + f.Config().TargetPort
}
func (f *SAMForwarder) sam() string {
return f.SamHost + ":" + f.SamPort
return f.Config().SamHost + ":" + f.Config().SamPort
}
func (f *SAMForwarder) HTTPRequestBytes(conn *sam3.SAMConn) ([]byte, *http.Request, error) {
@@ -303,7 +268,7 @@ func (f *SAMForwarder) forward(conn *sam3.SAMConn) { //(conn net.Conn) {
log.Fatalf("Dial failed: %v", err)
}
go func() {
if f.Type == "http" {
if f.Conf.Type == "http" {
defer f.clientUnlockAndClose(true, false, client)
defer f.connUnlockAndClose(false, true, conn)
if requestbytes, request, err = f.HTTPRequestBytes(conn); err == nil {
@@ -319,7 +284,7 @@ func (f *SAMForwarder) forward(conn *sam3.SAMConn) { //(conn net.Conn) {
}
}()
go func() {
if f.Type == "http" {
if f.Conf.Type == "http" {
defer f.clientUnlockAndClose(false, true, client)
defer f.connUnlockAndClose(true, false, conn)
if responsebytes, err = f.HTTPResponseBytes(client, request); err == nil {
@@ -355,9 +320,9 @@ func (f *SAMForwarder) Base64() string {
//Serve starts the SAM connection and and forwards the local host:port to i2p
func (f *SAMForwarder) Serve() error {
//lsk, lspk, lspsk := f.leasesetsettings()
//lsk, lspk, lspsk := f.Config().Leasesetsettings()
if f.Up() {
if f.publishStream, err = f.samConn.NewStreamSession(f.TunName, f.SamKeys, f.print()); err != nil {
if f.publishStream, err = f.samConn.NewStreamSession(f.Conf.TunName, f.SamKeys, f.print()); err != nil {
log.Println("Stream Creation error:", err.Error())
return err
}
@@ -403,18 +368,18 @@ func (s *SAMForwarder) Load() (samtunnel.SAMTunnel, error) {
return nil, err
}
log.Println("SAM Bridge connection established.")
if s.save {
if s.Conf.SaveFile {
log.Println("Saving i2p keys")
}
if s.SamKeys, err = sfi2pkeys.Load(s.FilePath, s.TunName, s.passfile, s.samConn, s.save); err != nil {
if s.SamKeys, err = sfi2pkeys.Load(s.Conf.FilePath, s.Conf.TunName, s.Conf.KeyFilePath, s.samConn, s.Conf.SaveFile); err != nil {
return nil, err
}
log.Println("Destination keys generated, tunnel name:", s.TunName)
if s.save {
if err := sfi2pkeys.Save(s.FilePath, s.TunName, s.passfile, s.SamKeys); err != nil {
log.Println("Destination keys generated, tunnel name:", s.Conf.TunName)
if s.Conf.SaveFile {
if err := sfi2pkeys.Save(s.Conf.FilePath, s.Conf.TunName, s.Conf.KeyFilePath, s.SamKeys); err != nil {
return nil, err
}
log.Println("Saved tunnel keys for", s.TunName)
log.Println("Saved tunnel keys for", s.Conf.TunName)
}
s.Hasher, err = hashhash.NewHasher(len(strings.Replace(s.Base32(), ".b32.i2p", "", 1)))
if err != nil {
@@ -432,39 +397,9 @@ func NewSAMForwarder(host, port string) (*SAMForwarder, error) {
//NewSAMForwarderFromOptions makes a new SAM forwarder with default options, accepts host:port arguments
func NewSAMForwarderFromOptions(opts ...func(*SAMForwarder) error) (*SAMForwarder, error) {
var s SAMForwarder
s.SamHost = "127.0.0.1"
s.SamPort = "7656"
s.FilePath = ""
s.save = false
s.TargetHost = "127.0.0.1"
s.TargetPort = "8081"
s.TunName = "samForwarder"
s.Type = "server"
s.inLength = "3"
s.outLength = "3"
s.inQuantity = "2"
s.outQuantity = "2"
s.inVariance = "1"
s.outVariance = "1"
s.inBackupQuantity = "3"
s.outBackupQuantity = "3"
s.inAllowZeroHop = "false"
s.outAllowZeroHop = "false"
s.encryptLeaseSet = "false"
s.leaseSetKey = ""
s.leaseSetPrivateKey = ""
s.leaseSetPrivateSigningKey = ""
s.fastRecieve = "false"
s.useCompression = "true"
s.reduceIdle = "false"
s.reduceIdleTime = "15"
s.reduceIdleQuantity = "4"
s.closeIdle = "false"
s.closeIdleTime = "300000"
s.Conf = i2ptunconf.NewI2PBlankTunConf()
s.clientLock = false
s.connLock = false
s.messageReliability = "none"
s.passfile = ""
for _, o := range opts {
if err := o(&s); err != nil {
return nil, err

View File

@@ -11,7 +11,7 @@ type ClientOption func(*SAMSSUClientForwarder) error
//SetClientFilePath sets the host of the SAMSSUForwarder's SAM bridge
func SetClientFilePath(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.FilePath = s
c.Conf.FilePath = s
return nil
}
}
@@ -19,7 +19,7 @@ func SetClientFilePath(s string) func(*SAMSSUClientForwarder) error {
//SetClientSaveFile tells the router to use an encrypted leaseset
func SetClientSaveFile(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.save = b
c.Conf.SaveFile = b
return nil
}
}
@@ -27,7 +27,7 @@ func SetClientSaveFile(b bool) func(*SAMSSUClientForwarder) error {
//SetClientHost sets the host of the SAMSSUForwarder's SAM bridge
func SetClientHost(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.TargetHost = s
c.Conf.TargetHost = s
return nil
}
}
@@ -40,7 +40,7 @@ func SetClientPort(s string) func(*SAMSSUClientForwarder) error {
return fmt.Errorf("Invalid SSU Client Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetPort = s
c.Conf.TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -50,7 +50,7 @@ func SetClientPort(s string) func(*SAMSSUClientForwarder) error {
//SetClientSAMHost sets the host of the SAMSSUForwarder's SAM bridge
func SetClientSAMHost(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -63,7 +63,7 @@ func SetClientSAMPort(s string) func(*SAMSSUClientForwarder) error {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -73,7 +73,7 @@ func SetClientSAMPort(s string) func(*SAMSSUClientForwarder) error {
//SetClientDestination sets the destination to forwarder SAMClientForwarder's to
func SetClientDestination(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.dest = s
c.Conf.ClientDest = s
return nil
}
}
@@ -81,7 +81,7 @@ func SetClientDestination(s string) func(*SAMSSUClientForwarder) error {
//SetClientName sets the host of the SAMSSUForwarder's SAM bridge
func SetClientName(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.TunName = s
c.Conf.TunName = s
return nil
}
}
@@ -90,19 +90,19 @@ func SetClientName(s string) func(*SAMSSUClientForwarder) error {
func SetClientSigType(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if s == "" {
c.sigType = ""
c.Conf.SigType = ""
} else if s == "DSA_SHA1" {
c.sigType = "DSA_SHA1"
c.Conf.SigType = "DSA_SHA1"
} else if s == "ECDSA_SHA256_P256" {
c.sigType = "ECDSA_SHA256_P256"
c.Conf.SigType = "ECDSA_SHA256_P256"
} else if s == "ECDSA_SHA384_P384" {
c.sigType = "ECDSA_SHA384_P384"
c.Conf.SigType = "ECDSA_SHA384_P384"
} else if s == "ECDSA_SHA512_P521" {
c.sigType = "ECDSA_SHA512_P521"
c.Conf.SigType = "ECDSA_SHA512_P521"
} else if s == "EdDSA_SHA512_Ed25519" {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
} else {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
}
return nil
}
@@ -112,7 +112,7 @@ func SetClientSigType(s string) func(*SAMSSUClientForwarder) error {
func SetClientInLength(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u < 7 && u >= 0 {
c.inLength = strconv.Itoa(u)
c.Conf.InLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -123,7 +123,7 @@ func SetClientInLength(u int) func(*SAMSSUClientForwarder) error {
func SetClientOutLength(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u < 7 && u >= 0 {
c.outLength = strconv.Itoa(u)
c.Conf.OutLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -134,7 +134,7 @@ func SetClientOutLength(u int) func(*SAMSSUClientForwarder) error {
func SetClientInVariance(i int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if i < 7 && i > -7 {
c.inVariance = strconv.Itoa(i)
c.Conf.InVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -145,7 +145,7 @@ func SetClientInVariance(i int) func(*SAMSSUClientForwarder) error {
func SetClientOutVariance(i int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if i < 7 && i > -7 {
c.outVariance = strconv.Itoa(i)
c.Conf.OutVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -156,7 +156,7 @@ func SetClientOutVariance(i int) func(*SAMSSUClientForwarder) error {
func SetClientInQuantity(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u <= 16 && u > 0 {
c.inQuantity = strconv.Itoa(u)
c.Conf.InQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -167,7 +167,7 @@ func SetClientInQuantity(u int) func(*SAMSSUClientForwarder) error {
func SetClientOutQuantity(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u <= 16 && u > 0 {
c.outQuantity = strconv.Itoa(u)
c.Conf.OutQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -178,7 +178,7 @@ func SetClientOutQuantity(u int) func(*SAMSSUClientForwarder) error {
func SetClientInBackups(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u < 6 && u >= 0 {
c.inBackupQuantity = strconv.Itoa(u)
c.Conf.InBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -189,7 +189,7 @@ func SetClientInBackups(u int) func(*SAMSSUClientForwarder) error {
func SetClientOutBackups(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u < 6 && u >= 0 {
c.outBackupQuantity = strconv.Itoa(u)
c.Conf.OutBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -200,10 +200,10 @@ func SetClientOutBackups(u int) func(*SAMSSUClientForwarder) error {
func SetClientEncrypt(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.encryptLeaseSet = "true"
c.Conf.EncryptLeaseSet = true
return nil
}
c.encryptLeaseSet = "false"
c.Conf.EncryptLeaseSet = false
return nil
}
}
@@ -211,7 +211,7 @@ func SetClientEncrypt(b bool) func(*SAMSSUClientForwarder) error {
//SetClientLeaseSetKey sets the host of the SAMForwarder's SAM bridge
func SetClientLeaseSetKey(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.leaseSetKey = s
c.Conf.LeaseSetKey = s
return nil
}
}
@@ -219,7 +219,7 @@ func SetClientLeaseSetKey(s string) func(*SAMSSUClientForwarder) error {
//SetClientLeaseSetPrivateKey sets the host of the SAMForwarder's SAM bridge
func SetClientLeaseSetPrivateKey(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.leaseSetPrivateKey = s
c.Conf.LeaseSetPrivateKey = s
return nil
}
}
@@ -227,7 +227,7 @@ func SetClientLeaseSetPrivateKey(s string) func(*SAMSSUClientForwarder) error {
//SetClientLeaseSetPrivateSigningKey sets the host of the SAMForwarder's SAM bridge
func SetClientLeaseSetPrivateSigningKey(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.leaseSetPrivateSigningKey = s
c.Conf.LeaseSetPrivateSigningKey = s
return nil
}
}
@@ -235,7 +235,7 @@ func SetClientLeaseSetPrivateSigningKey(s string) func(*SAMSSUClientForwarder) e
//SetClientMessageReliability sets
func SetClientMessageReliability(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.messageReliability = s
c.Conf.MessageReliability = s
return nil
}
}
@@ -244,10 +244,10 @@ func SetClientMessageReliability(s string) func(*SAMSSUClientForwarder) error {
func SetClientAllowZeroIn(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.inAllowZeroHop = "true"
c.Conf.InAllowZeroHop = true
return nil
}
c.inAllowZeroHop = "false"
c.Conf.InAllowZeroHop = false
return nil
}
}
@@ -256,10 +256,10 @@ func SetClientAllowZeroIn(b bool) func(*SAMSSUClientForwarder) error {
func SetClientAllowZeroOut(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.outAllowZeroHop = "true"
c.Conf.OutAllowZeroHop = true
return nil
}
c.outAllowZeroHop = "false"
c.Conf.OutAllowZeroHop = false
return nil
}
}
@@ -268,10 +268,10 @@ func SetClientAllowZeroOut(b bool) func(*SAMSSUClientForwarder) error {
func SetClientFastRecieve(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.fastRecieve = "true"
c.Conf.FastRecieve = true
return nil
}
c.fastRecieve = "false"
c.Conf.FastRecieve = false
return nil
}
}
@@ -280,10 +280,10 @@ func SetClientFastRecieve(b bool) func(*SAMSSUClientForwarder) error {
func SetClientCompress(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.useCompression = "true"
c.Conf.UseCompression = true
return nil
}
c.useCompression = "false"
c.Conf.UseCompression = false
return nil
}
}
@@ -292,10 +292,10 @@ func SetClientCompress(b bool) func(*SAMSSUClientForwarder) error {
func SetClientReduceIdle(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.reduceIdle = "true"
c.Conf.ReduceIdle = true
return nil
}
c.reduceIdle = "false"
c.Conf.ReduceIdle = false
return nil
}
}
@@ -303,9 +303,9 @@ func SetClientReduceIdle(b bool) func(*SAMSSUClientForwarder) error {
//SetClientReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetClientReduceIdleTime(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 6 {
c.reduceIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.ReduceIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes)")
@@ -315,9 +315,9 @@ func SetClientReduceIdleTime(u int) func(*SAMSSUClientForwarder) error {
//SetClientReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetClientReduceIdleTimeMs(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 300000 {
c.reduceIdleTime = strconv.Itoa(u)
c.Conf.ReduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes)")
@@ -328,7 +328,7 @@ func SetClientReduceIdleTimeMs(u int) func(*SAMSSUClientForwarder) error {
func SetClientReduceIdleQuantity(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if u < 5 {
c.reduceIdleQuantity = strconv.Itoa(u)
c.Conf.ReduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
@@ -339,10 +339,10 @@ func SetClientReduceIdleQuantity(u int) func(*SAMSSUClientForwarder) error {
func SetClientCloseIdle(b bool) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if b {
c.closeIdle = "true"
c.Conf.CloseIdle = true
return nil
}
c.closeIdle = "false"
c.Conf.CloseIdle = false
return nil
}
}
@@ -350,9 +350,9 @@ func SetClientCloseIdle(b bool) func(*SAMSSUClientForwarder) error {
//SetClientCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetClientCloseIdleTime(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 6 {
c.closeIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.CloseIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
@@ -362,9 +362,9 @@ func SetClientCloseIdleTime(u int) func(*SAMSSUClientForwarder) error {
//SetClientCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetClientCloseIdleTimeMs(u int) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 300000 {
c.closeIdleTime = strconv.Itoa(u)
c.Conf.CloseIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
@@ -375,16 +375,16 @@ func SetClientCloseIdleTimeMs(u int) func(*SAMSSUClientForwarder) error {
func SetClientAccessListType(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if s == "whitelist" {
c.accessListType = "whitelist"
c.Conf.AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.accessListType = "blacklist"
c.Conf.AccessListType = "blacklist"
return nil
} else if s == "none" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
} else if s == "" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
@@ -396,7 +396,7 @@ func SetClientAccessList(s []string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
if len(s) > 0 {
for _, a := range s {
c.accessList = append(c.accessList, a)
c.Conf.AccessList = append(c.Conf.AccessList, a)
}
return nil
}
@@ -407,7 +407,7 @@ func SetClientAccessList(s []string) func(*SAMSSUClientForwarder) error {
//SetKeyFile sets
func SetClientPassword(s string) func(*SAMSSUClientForwarder) error {
return func(c *SAMSSUClientForwarder) error {
c.passfile = s
c.Conf.KeyFilePath = s
return nil
}
}

View File

@@ -4,14 +4,13 @@ import (
"io"
"log"
"net"
//"os"
//"path/filepath"
"strconv"
"strings"
"time"
)
import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam-forwarder/interface"
@@ -22,66 +21,30 @@ import (
//SAMSSUClientForwarder is a structure which automatically configured the forwarding of
//a local port to i2p over the SAM API.
type SAMSSUClientForwarder struct {
SamHost string
SamPort string
TunName string
Type string
TargetHost string
TargetPort string
samConn *sam3.SAM
SamKeys i2pkeys.I2PKeys
Hasher *hashhash.Hasher
connectStream *sam3.DatagramSession
dest string
addr i2pkeys.I2PAddr
publishConnection net.PacketConn
FilePath string
file io.ReadWriter
save bool
up bool
file io.ReadWriter
up bool
// samcatd options
passfile string
sigType string
// config
Conf *i2ptunconf.Conf
}
// I2CP options
encryptLeaseSet string
leaseSetKey string
leaseSetPrivateKey string
leaseSetPrivateSigningKey string
inAllowZeroHop string
outAllowZeroHop string
inLength string
outLength string
inQuantity string
outQuantity string
inVariance string
outVariance string
inBackupQuantity string
outBackupQuantity string
fastRecieve string
useCompression string
messageReliability string
closeIdle string
closeIdleTime string
reduceIdle string
reduceIdleTime string
reduceIdleQuantity string
//Streaming Library options
accessListType string
accessList []string
func (f *SAMSSUClientForwarder) Config() *i2ptunconf.Conf {
return f.Conf
}
func (f *SAMSSUClientForwarder) GetType() string {
return f.Type
return f.Config().Type
}
func (f *SAMSSUClientForwarder) ID() string {
return f.TunName
return f.Config().TunName
}
func (f *SAMSSUClientForwarder) Keys() i2pkeys.I2PKeys {
@@ -101,32 +64,7 @@ func (f *SAMSSUClientForwarder) Close() error {
}
func (f *SAMSSUClientForwarder) print() []string {
lsk, lspk, lspsk := f.leasesetsettings()
return []string{
//f.targetForPort443(),
"inbound.length=" + f.inLength,
"outbound.length=" + f.outLength,
"inbound.lengthVariance=" + f.inVariance,
"outbound.lengthVariance=" + f.outVariance,
"inbound.backupQuantity=" + f.inBackupQuantity,
"outbound.backupQuantity=" + f.outBackupQuantity,
"inbound.quantity=" + f.inQuantity,
"outbound.quantity=" + f.outQuantity,
"inbound.allowZeroHop=" + f.inAllowZeroHop,
"outbound.allowZeroHop=" + f.outAllowZeroHop,
"i2cp.fastRecieve=" + f.fastRecieve,
"i2cp.gzip=" + f.useCompression,
"i2cp.reduceOnIdle=" + f.reduceIdle,
"i2cp.reduceIdleTime=" + f.reduceIdleTime,
"i2cp.reduceQuantity=" + f.reduceIdleQuantity,
"i2cp.closeOnIdle=" + f.closeIdle,
"i2cp.closeIdleTime=" + f.closeIdleTime,
"i2cp.messageReliability=" + f.messageReliability,
"i2cp.encryptLeaseSet=" + f.encryptLeaseSet,
lsk, lspk, lspsk,
f.accesslisttype(),
f.accesslist(),
}
return f.Config().PrintSlice()
}
func (f *SAMSSUClientForwarder) Props() map[string]string {
@@ -144,11 +82,11 @@ func (f *SAMSSUClientForwarder) Props() map[string]string {
func (f *SAMSSUClientForwarder) Print() string {
var r string
r += "name=" + f.TunName + "\n"
r += "type=" + f.Type + "\n"
r += "name=" + f.Config().TunName + "\n"
r += "type=" + f.Config().Type + "\n"
r += "base32=" + f.Base32() + "\n"
r += "base64=" + f.Base64() + "\n"
r += "dest=" + f.dest + "\n"
r += "dest=" + f.Config().ClientDest + "\n"
r += "ssuclient\n"
for _, s := range f.print() {
r += s + "\n"
@@ -170,20 +108,20 @@ func (f *SAMSSUClientForwarder) Search(search string) string {
}
func (f *SAMSSUClientForwarder) accesslisttype() string {
if f.accessListType == "whitelist" {
if f.Config().AccessListType == "whitelist" {
return "i2cp.enableAccessList=true"
} else if f.accessListType == "blacklist" {
} else if f.Config().AccessListType == "blacklist" {
return "i2cp.enableBlackList=true"
} else if f.accessListType == "none" {
} else if f.Config().AccessListType == "none" {
return ""
}
return ""
}
func (f *SAMSSUClientForwarder) accesslist() string {
if f.accessListType != "" && len(f.accessList) > 0 {
if f.Config().AccessListType != "" && len(f.Config().AccessList) > 0 {
r := ""
for _, s := range f.accessList {
for _, s := range f.Config().AccessList {
r += s + ","
}
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
@@ -193,14 +131,14 @@ func (f *SAMSSUClientForwarder) accesslist() string {
func (f *SAMSSUClientForwarder) leasesetsettings() (string, string, string) {
var r, s, t string
if f.leaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.leaseSetKey
if f.Config().LeaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.Config().LeaseSetKey
}
if f.leaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.leaseSetPrivateKey
if f.Config().LeaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.Config().LeaseSetPrivateKey
}
if f.leaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.leaseSetPrivateSigningKey
if f.Config().LeaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.Config().LeaseSetPrivateSigningKey
}
return r, s, t
}
@@ -212,11 +150,11 @@ func (f *SAMSSUClientForwarder) Destination() string {
// Target returns the host:port of the local service you want to forward to i2p
func (f *SAMSSUClientForwarder) Target() string {
return f.TargetHost + ":" + f.TargetPort
return f.Config().TargetHost + ":" + f.Config().TargetPort
}
func (f *SAMSSUClientForwarder) sam() string {
return f.SamHost + ":" + f.SamPort
return f.Config().SamHost + ":" + f.Config().SamPort
}
//Base32 returns the base32 address of the local destination
@@ -279,13 +217,13 @@ func (f *SAMSSUClientForwarder) errSleep(err error) bool {
func (f *SAMSSUClientForwarder) Serve() error {
var err error
log.Println("Establishing a SAM datagram session.")
if f.publishConnection, err = net.ListenPacket("udp", f.Target()); err != nil {
if f.publishConnection, err = net.ListenPacket("udp", f.Config().Target()); err != nil {
return err
}
//p, _ := strconv.Atoi(f.TargetPort)
sp, _ := strconv.Atoi(f.SamPort)
//p, _ := strconv.Atoi(f.Config().TargetPort)
sp, _ := strconv.Atoi(f.Config().SamPort)
f.connectStream, err = f.samConn.NewDatagramSession(
f.TunName,
f.Config().TunName,
f.SamKeys,
f.print(),
sp-1,
@@ -298,7 +236,7 @@ func (f *SAMSSUClientForwarder) Serve() error {
log.Println("Human-readable hash of Client:\n ", f.Base32Readable())
/* Close := false
for !Close {
//addr, err := net.ResolveUDPAddr("udp", f.Target())
//addr, err := net.ResolveUDPAddr("udp", f.Config().Target())
Close = f.errSleep(err)
//f.publishConnection, err = net.DialUDP("udp", nil, addr)
Close = f.errSleep(err)
@@ -314,22 +252,22 @@ func (s *SAMSSUClientForwarder) Load() (samtunnel.SAMTunnel, error) {
if s.samConn, err = sam3.NewSAM(s.sam()); err != nil {
return nil, err
}
if s.addr, err = s.samConn.Lookup(s.dest); err != nil {
if s.addr, err = s.samConn.Lookup(s.Config().ClientDest); err != nil {
return nil, err
}
log.Println("SAM Bridge connection established.")
if s.save {
if s.Config().SaveFile {
log.Println("Saving i2p keys")
}
if s.SamKeys, err = sfi2pkeys.Load(s.FilePath, s.TunName, s.passfile, s.samConn, s.save); err != nil {
if s.SamKeys, err = sfi2pkeys.Load(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.samConn, s.Config().SaveFile); err != nil {
return nil, err
}
log.Println("Destination keys generated, tunnel name:", s.TunName)
if s.save {
if err := sfi2pkeys.Save(s.FilePath, s.TunName, s.passfile, s.SamKeys); err != nil {
log.Println("Destination keys generated, tunnel name:", s.Config().TunName)
if s.Config().SaveFile {
if err := sfi2pkeys.Save(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.SamKeys); err != nil {
return nil, err
}
log.Println("Saved tunnel keys for", s.TunName)
log.Println("Saved tunnel keys for", s.Config().TunName)
}
s.Hasher, err = hashhash.NewHasher(len(strings.Replace(s.Base32(), ".b32.i2p", "", 1)))
if err != nil {
@@ -342,39 +280,8 @@ func (s *SAMSSUClientForwarder) Load() (samtunnel.SAMTunnel, error) {
//NewSAMSSUClientForwarderFromOptions makes a new SAM forwarder with default options, accepts host:port arguments
func NewSAMSSUClientForwarderFromOptions(opts ...func(*SAMSSUClientForwarder) error) (*SAMSSUClientForwarder, error) {
var s SAMSSUClientForwarder
s.SamHost = "127.0.0.1"
s.SamPort = "7656"
s.FilePath = ""
s.save = false
s.TargetHost = "127.0.0.1"
s.TargetPort = "0"
s.TunName = "samSSUForwarder"
s.inLength = "3"
s.outLength = "3"
s.inQuantity = "2"
s.outQuantity = "2"
s.inVariance = "1"
s.outVariance = "1"
s.inBackupQuantity = "3"
s.outBackupQuantity = "3"
s.inAllowZeroHop = "false"
s.outAllowZeroHop = "false"
s.fastRecieve = "false"
s.useCompression = "true"
s.encryptLeaseSet = "false"
s.leaseSetKey = ""
s.leaseSetPrivateKey = ""
s.leaseSetPrivateSigningKey = ""
s.reduceIdle = "false"
s.reduceIdleTime = "15"
s.closeIdle = "false"
s.closeIdleTime = "30"
s.reduceIdleQuantity = "4"
s.dest = "none"
s.Type = "udpclient"
s.messageReliability = "none"
s.passfile = ""
s.dest = "i2p-projekt.i2p"
s.Conf = i2ptunconf.NewI2PBlankTunConf()
s.Conf.Type = "udpclient"
for _, o := range opts {
if err := o(&s); err != nil {
return nil, err

View File

@@ -11,7 +11,7 @@ type Option func(*SAMSSUForwarder) error
//SetFilePath sets the host of the SAMSSUForwarder's SAM bridge
func SetFilePath(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.FilePath = s
c.Conf.FilePath = s
return nil
}
}
@@ -19,7 +19,7 @@ func SetFilePath(s string) func(*SAMSSUForwarder) error {
//SetSaveFile tells the router to use an encrypted leaseset
func SetSaveFile(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.save = b
c.Conf.SaveFile = b
return nil
}
}
@@ -27,7 +27,7 @@ func SetSaveFile(b bool) func(*SAMSSUForwarder) error {
//SetHost sets the host of the SAMSSUForwarder's SAM bridge
func SetHost(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.TargetHost = s
c.Conf.TargetHost = s
return nil
}
}
@@ -40,7 +40,7 @@ func SetPort(s string) func(*SAMSSUForwarder) error {
return fmt.Errorf("Invalid SSU Server Target Port %s; non-number ", s)
}
if port < 65536 && port > -1 {
c.TargetPort = s
c.Conf.TargetPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -50,7 +50,7 @@ func SetPort(s string) func(*SAMSSUForwarder) error {
//SetSAMHost sets the host of the SAMSSUForwarder's SAM bridge
func SetSAMHost(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.SamHost = s
c.Conf.SamHost = s
return nil
}
}
@@ -63,7 +63,7 @@ func SetSAMPort(s string) func(*SAMSSUForwarder) error {
return fmt.Errorf("Invalid SAM Port %s; non-number", s)
}
if port < 65536 && port > -1 {
c.SamPort = s
c.Conf.SamPort = s
return nil
}
return fmt.Errorf("Invalid port")
@@ -73,7 +73,7 @@ func SetSAMPort(s string) func(*SAMSSUForwarder) error {
//SetName sets the host of the SAMSSUForwarder's SAM bridge
func SetName(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.TunName = s
c.Conf.TunName = s
return nil
}
}
@@ -82,19 +82,19 @@ func SetName(s string) func(*SAMSSUForwarder) error {
func SetSigType(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if s == "" {
c.sigType = ""
c.Conf.SigType = ""
} else if s == "DSA_SHA1" {
c.sigType = "DSA_SHA1"
c.Conf.SigType = "DSA_SHA1"
} else if s == "ECDSA_SHA256_P256" {
c.sigType = "ECDSA_SHA256_P256"
c.Conf.SigType = "ECDSA_SHA256_P256"
} else if s == "ECDSA_SHA384_P384" {
c.sigType = "ECDSA_SHA384_P384"
c.Conf.SigType = "ECDSA_SHA384_P384"
} else if s == "ECDSA_SHA512_P521" {
c.sigType = "ECDSA_SHA512_P521"
c.Conf.SigType = "ECDSA_SHA512_P521"
} else if s == "EdDSA_SHA512_Ed25519" {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
} else {
c.sigType = "EdDSA_SHA512_Ed25519"
c.Conf.SigType = "EdDSA_SHA512_Ed25519"
}
return nil
}
@@ -104,7 +104,7 @@ func SetSigType(s string) func(*SAMSSUForwarder) error {
func SetInLength(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u < 7 && u >= 0 {
c.inLength = strconv.Itoa(u)
c.Conf.InLength = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -115,7 +115,7 @@ func SetInLength(u int) func(*SAMSSUForwarder) error {
func SetOutLength(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u < 7 && u >= 0 {
c.outLength = strconv.Itoa(u)
c.Conf.OutLength = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel length")
@@ -126,7 +126,7 @@ func SetOutLength(u int) func(*SAMSSUForwarder) error {
func SetInVariance(i int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if i < 7 && i > -7 {
c.inVariance = strconv.Itoa(i)
c.Conf.InVariance = i
return nil
}
return fmt.Errorf("Invalid inbound tunnel length")
@@ -137,7 +137,7 @@ func SetInVariance(i int) func(*SAMSSUForwarder) error {
func SetOutVariance(i int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if i < 7 && i > -7 {
c.outVariance = strconv.Itoa(i)
c.Conf.OutVariance = i
return nil
}
return fmt.Errorf("Invalid outbound tunnel variance")
@@ -148,7 +148,7 @@ func SetOutVariance(i int) func(*SAMSSUForwarder) error {
func SetInQuantity(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u <= 16 && u > 0 {
c.inQuantity = strconv.Itoa(u)
c.Conf.InQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel quantity")
@@ -159,7 +159,7 @@ func SetInQuantity(u int) func(*SAMSSUForwarder) error {
func SetOutQuantity(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u <= 16 && u > 0 {
c.outQuantity = strconv.Itoa(u)
c.Conf.OutQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel quantity")
@@ -170,7 +170,7 @@ func SetOutQuantity(u int) func(*SAMSSUForwarder) error {
func SetInBackups(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u < 6 && u >= 0 {
c.inBackupQuantity = strconv.Itoa(u)
c.Conf.InBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid inbound tunnel backup quantity")
@@ -181,7 +181,7 @@ func SetInBackups(u int) func(*SAMSSUForwarder) error {
func SetOutBackups(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u < 6 && u >= 0 {
c.outBackupQuantity = strconv.Itoa(u)
c.Conf.OutBackupQuantity = u
return nil
}
return fmt.Errorf("Invalid outbound tunnel backup quantity")
@@ -192,10 +192,10 @@ func SetOutBackups(u int) func(*SAMSSUForwarder) error {
func SetEncrypt(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.encryptLeaseSet = "true"
c.Conf.EncryptLeaseSet = true
return nil
}
c.encryptLeaseSet = "false"
c.Conf.EncryptLeaseSet = false
return nil
}
}
@@ -203,7 +203,7 @@ func SetEncrypt(b bool) func(*SAMSSUForwarder) error {
//SetLeaseSetKey sets
func SetLeaseSetKey(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.leaseSetKey = s
c.Conf.LeaseSetKey = s
return nil
}
}
@@ -211,7 +211,7 @@ func SetLeaseSetKey(s string) func(*SAMSSUForwarder) error {
//SetLeaseSetPrivateKey sets
func SetLeaseSetPrivateKey(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.leaseSetPrivateKey = s
c.Conf.LeaseSetPrivateKey = s
return nil
}
}
@@ -219,7 +219,7 @@ func SetLeaseSetPrivateKey(s string) func(*SAMSSUForwarder) error {
//SetLeaseSetPrivateSigningKey sets
func SetLeaseSetPrivateSigningKey(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.leaseSetPrivateSigningKey = s
c.Conf.LeaseSetPrivateSigningKey = s
return nil
}
}
@@ -227,7 +227,7 @@ func SetLeaseSetPrivateSigningKey(s string) func(*SAMSSUForwarder) error {
//SetMessageReliability sets
func SetMessageReliability(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.messageReliability = s
c.Conf.MessageReliability = s
return nil
}
}
@@ -236,10 +236,10 @@ func SetMessageReliability(s string) func(*SAMSSUForwarder) error {
func SetAllowZeroIn(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.inAllowZeroHop = "true"
c.Conf.InAllowZeroHop = true
return nil
}
c.inAllowZeroHop = "false"
c.Conf.InAllowZeroHop = false
return nil
}
}
@@ -248,10 +248,10 @@ func SetAllowZeroIn(b bool) func(*SAMSSUForwarder) error {
func SetAllowZeroOut(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.outAllowZeroHop = "true"
c.Conf.OutAllowZeroHop = true
return nil
}
c.outAllowZeroHop = "false"
c.Conf.OutAllowZeroHop = false
return nil
}
}
@@ -260,10 +260,10 @@ func SetAllowZeroOut(b bool) func(*SAMSSUForwarder) error {
func SetFastRecieve(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.fastRecieve = "true"
c.Conf.FastRecieve = true
return nil
}
c.fastRecieve = "false"
c.Conf.FastRecieve = false
return nil
}
}
@@ -272,10 +272,10 @@ func SetFastRecieve(b bool) func(*SAMSSUForwarder) error {
func SetCompress(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.useCompression = "true"
c.Conf.UseCompression = true
return nil
}
c.useCompression = "false"
c.Conf.UseCompression = false
return nil
}
}
@@ -284,10 +284,10 @@ func SetCompress(b bool) func(*SAMSSUForwarder) error {
func SetReduceIdle(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.reduceIdle = "true"
c.Conf.ReduceIdle = true
return nil
}
c.reduceIdle = "false"
c.Conf.ReduceIdle = false
return nil
}
}
@@ -295,9 +295,9 @@ func SetReduceIdle(b bool) func(*SAMSSUForwarder) error {
//SetReduceIdleTime sets the time to wait before reducing tunnels to idle levels
func SetReduceIdleTime(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 6 {
c.reduceIdleTime = strconv.Itoa((u * 60) * 1000)
c.Conf.ReduceIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in minutes) %v", u)
@@ -307,9 +307,9 @@ func SetReduceIdleTime(u int) func(*SAMSSUForwarder) error {
//SetReduceIdleTimeMs sets the time to wait before reducing tunnels to idle levels in milliseconds
func SetReduceIdleTimeMs(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.reduceIdleTime = "300000"
c.Conf.ReduceIdleTime = 300000
if u >= 300000 {
c.reduceIdleTime = strconv.Itoa(u)
c.Conf.ReduceIdleTime = u
return nil
}
return fmt.Errorf("Invalid close idle timeout(Measured in milliseconds) %v", u)
@@ -320,7 +320,7 @@ func SetReduceIdleTimeMs(u int) func(*SAMSSUForwarder) error {
func SetReduceIdleQuantity(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if u < 5 {
c.reduceIdleQuantity = strconv.Itoa(u)
c.Conf.ReduceIdleQuantity = u
return nil
}
return fmt.Errorf("Invalid reduce tunnel quantity")
@@ -331,10 +331,10 @@ func SetReduceIdleQuantity(u int) func(*SAMSSUForwarder) error {
func SetCloseIdle(b bool) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if b {
c.closeIdle = "true"
c.Conf.CloseIdle = true
return nil
}
c.closeIdle = "false"
c.Conf.CloseIdle = false
return nil
}
}
@@ -342,9 +342,9 @@ func SetCloseIdle(b bool) func(*SAMSSUForwarder) error {
//SetCloseIdleTime sets the time to wait before closing tunnels to idle levels
func SetCloseIdleTime(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 6 {
c.closeIdleTime = strconv.Itoa((u * 60) * 2000)
c.Conf.CloseIdleTime = (u * 60) * 1000
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes)")
@@ -354,9 +354,9 @@ func SetCloseIdleTime(u int) func(*SAMSSUForwarder) error {
//SetCloseIdleTimeMs sets the time to wait before closing tunnels to idle levels in milliseconds
func SetCloseIdleTimeMs(u int) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.closeIdleTime = "300000"
c.Conf.CloseIdleTime = 300000
if u >= 300000 {
c.closeIdleTime = strconv.Itoa(u)
c.Conf.CloseIdleTime = u
return nil
}
return fmt.Errorf("Invalid reduce idle timeout(Measured in minutes)")
@@ -367,16 +367,16 @@ func SetCloseIdleTimeMs(u int) func(*SAMSSUForwarder) error {
func SetAccessListType(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if s == "whitelist" {
c.accessListType = "whitelist"
c.Conf.AccessListType = "whitelist"
return nil
} else if s == "blacklist" {
c.accessListType = "blacklist"
c.Conf.AccessListType = "blacklist"
return nil
} else if s == "none" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
} else if s == "" {
c.accessListType = ""
c.Conf.AccessListType = ""
return nil
}
return fmt.Errorf("Invalid Access list type(whitelist, blacklist, none)")
@@ -388,7 +388,7 @@ func SetAccessList(s []string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
if len(s) > 0 {
for _, a := range s {
c.accessList = append(c.accessList, a)
c.Conf.AccessList = append(c.Conf.AccessList, a)
}
return nil
}
@@ -399,7 +399,7 @@ func SetAccessList(s []string) func(*SAMSSUForwarder) error {
//SetKeyFile sets
func SetKeyFile(s string) func(*SAMSSUForwarder) error {
return func(c *SAMSSUForwarder) error {
c.passfile = s
c.Conf.KeyFilePath = s
return nil
}
}

View File

@@ -4,14 +4,13 @@ import (
"io"
"log"
"net"
//"os"
//"path/filepath"
"strconv"
"strings"
"time"
)
import (
"github.com/eyedeekay/sam-forwarder/config"
"github.com/eyedeekay/sam-forwarder/hashhash"
"github.com/eyedeekay/sam-forwarder/i2pkeys"
"github.com/eyedeekay/sam-forwarder/interface"
@@ -22,66 +21,31 @@ import (
//SAMSSUForwarder is a structure which automatically configured the forwarding of
//a local service to i2p over the SAM API.
type SAMSSUForwarder struct {
SamHost string
SamPort string
TunName string
Type string
TargetHost string
TargetPort string
samConn *sam3.SAM
SamKeys i2pkeys.I2PKeys
Hasher *hashhash.Hasher
publishConnection *sam3.DatagramSession
clientConnection net.PacketConn
FilePath string
file io.ReadWriter
save bool
up bool
file io.ReadWriter
up bool
// samcatd options
passfile string
sigType string
// I2CP options
encryptLeaseSet string
leaseSetKey string
leaseSetPrivateKey string
leaseSetPrivateSigningKey string
inAllowZeroHop string
outAllowZeroHop string
inLength string
outLength string
inQuantity string
outQuantity string
inVariance string
outVariance string
inBackupQuantity string
outBackupQuantity string
fastRecieve string
useCompression string
messageReliability string
closeIdle string
closeIdleTime string
reduceIdle string
reduceIdleTime string
reduceIdleQuantity string
//Streaming Library options
accessListType string
accessList []string
// config
Conf *i2ptunconf.Conf
}
var err error
func (f *SAMSSUForwarder) Config() *i2ptunconf.Conf {
return f.Conf
}
func (f *SAMSSUForwarder) GetType() string {
return f.Type
return f.Config().Type
}
func (f *SAMSSUForwarder) ID() string {
return f.TunName
return f.Config().TunName
}
func (f *SAMSSUForwarder) Keys() i2pkeys.I2PKeys {
@@ -99,7 +63,7 @@ func (f *SAMSSUForwarder) Close() error {
}
func (f *SAMSSUForwarder) print() []string {
lsk, lspk, lspsk := f.leasesetsettings()
/*lsk, lspk, lspsk := f.leasesetsettings()
return []string{
//f.targetForPort443(),
"inbound.length=" + f.inLength,
@@ -124,7 +88,8 @@ func (f *SAMSSUForwarder) print() []string {
lsk, lspk, lspsk,
f.accesslisttype(),
f.accesslist(),
}
}*/
return f.Config().PrintSlice()
}
func (f *SAMSSUForwarder) Props() map[string]string {
@@ -142,8 +107,8 @@ func (f *SAMSSUForwarder) Props() map[string]string {
func (f *SAMSSUForwarder) Print() string {
var r string
r += "name=" + f.TunName + "\n"
r += "type=" + f.Type + "\n"
r += "name=" + f.Config().TunName + "\n"
r += "type=" + f.Config().Type + "\n"
r += "base32=" + f.Base32() + "\n"
r += "base64=" + f.Base64() + "\n"
r += "ssuserver\n"
@@ -167,20 +132,20 @@ func (f *SAMSSUForwarder) Search(search string) string {
}
func (f *SAMSSUForwarder) accesslisttype() string {
if f.accessListType == "whitelist" {
if f.Config().AccessListType == "whitelist" {
return "i2cp.enableAccessList=true"
} else if f.accessListType == "blacklist" {
} else if f.Config().AccessListType == "blacklist" {
return "i2cp.enableBlackList=true"
} else if f.accessListType == "none" {
} else if f.Config().AccessListType == "none" {
return ""
}
return ""
}
func (f *SAMSSUForwarder) accesslist() string {
if f.accessListType != "" && len(f.accessList) > 0 {
if f.Config().AccessListType != "" && len(f.Config().AccessList) > 0 {
r := ""
for _, s := range f.accessList {
for _, s := range f.Config().AccessList {
r += s + ","
}
return "i2cp.accessList=" + strings.TrimSuffix(r, ",")
@@ -190,25 +155,25 @@ func (f *SAMSSUForwarder) accesslist() string {
func (f *SAMSSUForwarder) leasesetsettings() (string, string, string) {
var r, s, t string
if f.leaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.leaseSetKey
if f.Config().LeaseSetKey != "" {
r = "i2cp.leaseSetKey=" + f.Config().LeaseSetKey
}
if f.leaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.leaseSetPrivateKey
if f.Config().LeaseSetPrivateKey != "" {
s = "i2cp.leaseSetPrivateKey=" + f.Config().LeaseSetPrivateKey
}
if f.leaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.leaseSetPrivateSigningKey
if f.Config().LeaseSetPrivateSigningKey != "" {
t = "i2cp.leaseSetPrivateSigningKey=" + f.Config().LeaseSetPrivateSigningKey
}
return r, s, t
}
// Target returns the host:port of the local service you want to forward to i2p
func (f *SAMSSUForwarder) Target() string {
return f.TargetHost + ":" + f.TargetPort
return f.Config().TargetHost + ":" + f.Config().TargetPort
}
func (f *SAMSSUForwarder) sam() string {
return f.SamHost + ":" + f.SamPort
return f.Config().SamHost + ":" + f.Config().SamPort
}
//func (f *SAMSSUForwarder) forward(conn net.Conn) {
@@ -216,10 +181,10 @@ func (f *SAMSSUForwarder) forward() {
Loop := false
if f.clientConnection == nil {
for !Loop {
log.Println("Attempting to resolve local UDP Service to forward to I2P", f.Target())
addr, err := net.ResolveUDPAddr("udp", f.Target())
log.Println("Attempting to resolve local UDP Service to forward to I2P", f.Config().Target())
addr, err := net.ResolveUDPAddr("udp", f.Config().Target())
Loop = f.errSleep(err)
log.Println("Attempting to dial resolved UDP Address", f.Target())
log.Println("Attempting to dial resolved UDP Address", f.Config().Target())
f.clientConnection, err = net.DialUDP("udp", nil, addr)
Loop = f.errSleep(err)
log.Printf("Connected %v to localhost %v\n", f.publishConnection, f.clientConnection)
@@ -279,9 +244,9 @@ func (f *SAMSSUForwarder) errSleep(err error) bool {
func (f *SAMSSUForwarder) Serve() error {
var err error
sp, _ := strconv.Atoi(f.SamPort)
sp, _ := strconv.Atoi(f.Config().SamPort)
f.publishConnection, err = f.samConn.NewDatagramSession(
f.TunName,
f.Config().TunName,
f.SamKeys,
f.print(),
sp-1,
@@ -306,18 +271,18 @@ func (s *SAMSSUForwarder) Load() (samtunnel.SAMTunnel, error) {
return nil, err
}
log.Println("SAM Bridge connection established.")
if s.save {
if s.Config().SaveFile {
log.Println("Saving i2p keys")
}
if s.SamKeys, err = sfi2pkeys.Load(s.FilePath, s.TunName, s.passfile, s.samConn, s.save); err != nil {
if s.SamKeys, err = sfi2pkeys.Load(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.samConn, s.Config().SaveFile); err != nil {
return nil, err
}
log.Println("Destination keys generated, tunnel name:", s.TunName)
if s.save {
if err := sfi2pkeys.Save(s.FilePath, s.TunName, s.passfile, s.SamKeys); err != nil {
log.Println("Destination keys generated, tunnel name:", s.Config().TunName)
if s.Config().SaveFile {
if err := sfi2pkeys.Save(s.Config().FilePath, s.Config().TunName, s.Config().KeyFilePath, s.SamKeys); err != nil {
return nil, err
}
log.Println("Saved tunnel keys for", s.TunName)
log.Println("Saved tunnel keys for", s.Config().TunName)
}
s.Hasher, err = hashhash.NewHasher(len(strings.Replace(s.Base32(), ".b32.i2p", "", 1)))
if err != nil {
@@ -339,37 +304,8 @@ func NewSAMSSUForwarder(host, port string) (*SAMSSUForwarder, error) {
//NewSAMSSUForwarderFromOptions makes a new SAM forwarder with default options, accepts host:port arguments
func NewSAMSSUForwarderFromOptions(opts ...func(*SAMSSUForwarder) error) (*SAMSSUForwarder, error) {
var s SAMSSUForwarder
s.SamHost = "127.0.0.1"
s.SamPort = "7656"
s.FilePath = ""
s.save = false
s.TargetHost = "127.0.0.1"
s.TargetPort = "8081"
s.TunName = "samSSUForwarder"
s.inLength = "3"
s.outLength = "3"
s.inQuantity = "2"
s.outQuantity = "2"
s.inVariance = "1"
s.outVariance = "1"
s.inBackupQuantity = "3"
s.outBackupQuantity = "3"
s.inAllowZeroHop = "false"
s.outAllowZeroHop = "false"
s.fastRecieve = "false"
s.useCompression = "true"
s.encryptLeaseSet = "false"
s.leaseSetKey = ""
s.leaseSetPrivateKey = ""
s.leaseSetPrivateSigningKey = ""
s.reduceIdle = "false"
s.reduceIdleTime = "300000"
s.closeIdle = "false"
s.closeIdleTime = "300000"
s.reduceIdleQuantity = "4"
s.Type = "udpserver"
s.messageReliability = "none"
s.passfile = ""
s.Conf = i2ptunconf.NewI2PBlankTunConf()
s.Conf.Type = "udpserver"
for _, o := range opts {
if err := o(&s); err != nil {
return nil, err