diff --git a/lib/http/client/new.go b/lib/http/client/new.go new file mode 100644 index 0000000..40d8622 --- /dev/null +++ b/lib/http/client/new.go @@ -0,0 +1,29 @@ +package httpclient + +import ( + "strings" + + i2pconv "github.com/go-i2p/go-i2ptunnel-config/lib" + i2ptunnel "github.com/go-i2p/go-i2ptunnel/lib/core" + "github.com/go-i2p/onramp" +) + +// NewHTTPClient creates a new HTTP Client tunnel with the given configuration +func NewHTTPClient(config i2pconv.TunnelConfig, samAddr string) (*HTTPClient, error) { + keys, options, err := config.SAMTunnel() + if err != nil { + return nil, err + } + name := strings.ReplaceAll(config.Name, " ", "_") + garlic, err := onramp.NewGarlic(name, samAddr, options) + if err != nil { + return nil, err + } + garlic.ServiceKeys = keys + return &HTTPClient{ + TunnelConfig: config, + Garlic: garlic, + I2PTunnelStatus: i2ptunnel.I2PTunnelStatusStopped, + done: make(chan struct{}), + }, nil +} diff --git a/lib/http/server/new.go b/lib/http/server/new.go new file mode 100644 index 0000000..88447b8 --- /dev/null +++ b/lib/http/server/new.go @@ -0,0 +1,43 @@ +package httpserver + +import ( + "net" + "strconv" + "strings" + + i2pconv "github.com/go-i2p/go-i2ptunnel-config/lib" + i2ptunnel "github.com/go-i2p/go-i2ptunnel/lib/core" + limitedlistener "github.com/go-i2p/go-limit" + "github.com/go-i2p/onramp" +) + +// NewHTTPServer creates a new HTTP Server tunnel with the given configuration +func NewHTTPServer(config i2pconv.TunnelConfig, samAddr string) (*HTTPServer, error) { + keys, options, err := config.SAMTunnel() + if err != nil { + return nil, err + } + name := strings.ReplaceAll(config.Name, " ", "_") + garlic, err := onramp.NewGarlic(name, samAddr, options) + if err != nil { + return nil, err + } + garlic.ServiceKeys = keys + localPort := strconv.Itoa(config.Port) + localAddr := net.JoinHostPort(config.Interface, localPort) + addr, err := net.ResolveTCPAddr("tcp", localAddr) + if err != nil { + return nil, err + } + return &HTTPServer{ + TunnelConfig: config, + Garlic: garlic, + Addr: addr, + I2PTunnelStatus: i2ptunnel.I2PTunnelStatusStopped, + LimitedConfig: limitedlistener.LimitedConfig{ + MaxConns: 1000, + RateLimit: 100, + }, + done: make(chan struct{}), + }, nil +}