Skip to content

Commit

Permalink
Added additional ports option
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Oct 14, 2020
1 parent 96a10c3 commit 1e6383f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
15 changes: 9 additions & 6 deletions cmd/diode/httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func init() {
httpdCmd.Flag.IntVar(&cfg.ProxyServerPort, "httpd_port", 80, "port of httpd server listening to")
httpdCmd.Flag.StringVar(&cfg.SProxyServerHost, "httpsd_host", "127.0.0.1", "host of httpsd server listening to")
httpdCmd.Flag.IntVar(&cfg.SProxyServerPort, "httpsd_port", 443, "port of httpsd server listening to")
httpdCmd.Flag.StringVar(&cfg.SProxyServerPorts, "additional_ports", "", "httpsd secure server ports")

httpdCmd.Flag.StringVar(&cfg.SProxyServerCertPath, "certpath", "./priv/cert.pem", "Pem format of certificate file path of httpsd secure server")
httpdCmd.Flag.StringVar(&cfg.SProxyServerPrivPath, "privpath", "./priv/priv.pem", "Pem format of private key file path of httpsd secure server")
httpdCmd.Flag.BoolVar(&cfg.EnableSProxyServer, "secure", false, "enable httpsd server")
Expand Down Expand Up @@ -75,12 +77,13 @@ func httpdHandler() (err error) {
}
proxyServer := rpc.NewProxyServer(socksServer)
proxyServer.SetConfig(rpc.ProxyConfig{
EnableSProxy: cfg.EnableSProxyServer,
ProxyServerAddr: cfg.ProxyServerAddr(),
SProxyServerAddr: cfg.SProxyServerAddr(),
CertPath: cfg.SProxyServerCertPath,
PrivPath: cfg.SProxyServerPrivPath,
AllowRedirect: cfg.AllowRedirectToSProxy,
EnableSProxy: cfg.EnableSProxyServer,
ProxyServerAddr: cfg.ProxyServerAddr(),
SProxyServerAddr: cfg.SProxyServerAddr(),
SProxyServerPorts: cfg.SProxyAdditionalPorts(),
CertPath: cfg.SProxyServerCertPath,
PrivPath: cfg.SProxyServerPrivPath,
AllowRedirect: cfg.AllowRedirectToSProxy,
})
// Start proxy server
app.SetProxyServer(proxyServer)
Expand Down
33 changes: 32 additions & 1 deletion config/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config
import (
"fmt"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -71,6 +72,7 @@ type Config struct {
ProxyServerPort int `yaml:"-" json:"-"`
SProxyServerHost string `yaml:"-" json:"-"`
SProxyServerPort int `yaml:"-" json:"-"`
SProxyServerPorts string `yaml:"-" json:"-"`
SProxyServerCertPath string `yaml:"-" json:"-"`
SProxyServerPrivPath string `yaml:"-" json:"-"`
AllowRedirectToSProxy bool `yaml:"-" json:"-"`
Expand Down Expand Up @@ -169,7 +171,36 @@ func (cfg *Config) ProxyServerAddr() string {

// SProxyServerAddr returns address that https proxy server listen to
func (cfg *Config) SProxyServerAddr() string {
return fmt.Sprintf("%s:%d", cfg.SProxyServerHost, cfg.SProxyServerPort)
return cfg.SProxyServerAddrForPort(cfg.SProxyServerPort)
}

// SProxyServerAddrForPort returns address that https proxy server listen to
func (cfg *Config) SProxyServerAddrForPort(port int) string {
return fmt.Sprintf("%s:%d", cfg.SProxyServerHost, port)
}

// SProxyAdditionalPorts returns the additional port numbers
func (cfg *Config) SProxyAdditionalPorts() []int {
var ports []int
items := strings.Split(cfg.SProxyServerPorts, ",")
for _, frag := range items {
ranges := strings.Split(frag, "..")
if len(ranges) == 2 {
start, err1 := strconv.Atoi(ranges[0])
end, err2 := strconv.Atoi(ranges[1])
if err1 == nil && err2 == nil {
for i := start; i <= end; i++ {
ports = append(ports, i)
}
}
} else {
portInt, err := strconv.Atoi(ranges[0])
if err == nil {
ports = append(ports, portInt)
}
}
}
return ports
}

// Bind struct for port forwarding
Expand Down
27 changes: 21 additions & 6 deletions rpc/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (

// Config is Proxy Server configuration
type ProxyConfig struct {
ProxyServerAddr string
SProxyServerAddr string
CertPath string
PrivPath string
EnableSProxy bool
AllowRedirect bool
ProxyServerAddr string
SProxyServerAddr string
SProxyServerPorts []int
CertPath string
PrivPath string
EnableSProxy bool
AllowRedirect bool
}

type HttpError struct {
Expand Down Expand Up @@ -248,6 +249,20 @@ func (proxyServer *ProxyServer) Start() error {
}
}
}()

if len(proxyServer.Config.SProxyServerPorts) > 0 {
proxyServer.logger.Info("Starting %d additional httpsd servers", len(proxyServer.Config.SProxyServerPorts))
}
for _, port := range proxyServer.Config.SProxyServerPorts {
addr := config.AppConfig.SProxyServerAddrForPort(port)
httpsServer := &http.Server{Addr: addr, Handler: httpsdHandler, TLSNextProto: protos}
go func() {
err := httpsServer.ListenAndServeTLS(proxyServer.Config.CertPath, proxyServer.Config.PrivPath)
if err != http.ErrServerClosed {
proxyServer.logger.Error("Couldn't start https proxy: %v", err)
}
}()
}
} else {
if proxyServer.httpsServer != nil {
proxyServer.httpsServer.Close()
Expand Down

0 comments on commit 1e6383f

Please sign in to comment.