Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Refactor options
Browse files Browse the repository at this point in the history
  • Loading branch information
bzEq committed Dec 25, 2023
1 parent 5c63b2f commit 436a39b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
36 changes: 10 additions & 26 deletions i3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,31 @@ import (
"math/rand"
"net"
"strings"
"sync"

"github.com/bzEq/bx/core"
"github.com/bzEq/bx/relayer"
)

var options struct {
Local string
LocalUDP string
LocalHTTPProxy string
Next string
ProtocolName string
NumConn int
UseTLS bool
NoUDP bool
}

func startRelayers() {
addrs := strings.Split(options.Local, ",")
var wg sync.WaitGroup
for _, addr := range addrs {
wg.Add(1)
go func(addr string) {
defer wg.Done()
startRelayer(addr)
}(addr)
}
wg.Wait()
}

func startRelayer(localAddr string) {
func startRelayer() {
r := &relayer.IntrinsicRelayer{}
r.Local = localAddr
r.Local = options.Local
r.LocalUDP = options.LocalUDP
r.NumUDPMux = 4
r.LocalHTTPProxy = options.LocalHTTPProxy
r.RelayProtocol = options.ProtocolName
r.NumUDPMux = options.NumConn
r.NoUDP = options.NoUDP
if options.Next != "" {
r.Next = strings.Split(options.Next, ",")
}
if options.UseTLS && len(r.Next) != 0 {
if options.UseTLS && !r.IsEndPoint() {
config := &tls.Config{InsecureSkipVerify: true, NextProtos: []string{options.ProtocolName}}
r.Dial = func(network, address string) (net.Conn, error) {
return tls.Dial(network, address, config)
Expand All @@ -63,7 +48,7 @@ func startRelayer(localAddr string) {
return net.Dial(network, address)
}
}
if options.UseTLS && len(r.Next) == 0 {
if options.UseTLS && r.IsEndPoint() {
config, err := core.CreateBarebonesTLSConfig(options.ProtocolName)
if err != nil {
log.Println(err)
Expand All @@ -86,17 +71,16 @@ func main() {
rand.Seed(seed)
var debug bool
flag.StringVar(&options.Local, "l", "localhost:1080", "Listen address of this relayer")
flag.StringVar(&options.LocalUDP, "u", "", "UDP listen address of this relayer")
flag.StringVar(&options.Next, "n", "", "Address of next-hop relayer")
flag.StringVar(&options.LocalHTTPProxy, "http_proxy", "", "Enable this relayer serving as http proxy")
flag.StringVar(&options.ProtocolName, "proto", "", "Name of relay protocol")
flag.BoolVar(&options.UseTLS, "use_tls", false, "Use TLS")
flag.IntVar(&options.NumConn, "j", 4, "Number of connections for UDP Mux")
flag.BoolVar(&options.NoUDP, "no_udp", false, "Disable UDP support")
flag.BoolVar(&options.UseTLS, "tls", false, "Wrap traffic in TLS")
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.Parse()
if !debug {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
startRelayers()
startRelayer()
}
1 change: 0 additions & 1 deletion j2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var options struct {
Local string
Next string
ProtocolName string
NumConn int
UseTLS bool
}

Expand Down
16 changes: 10 additions & 6 deletions relayer/intrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
type IntrinsicRelayer struct {
Listen func(string, string) (net.Listener, error)
Local string
LocalUDP string
NumUDPMux int
LocalHTTPProxy string
Dial func(string, string) (net.Conn, error)
Next []string
RelayProtocol string
NumUDPMux int
NoUDP bool
udpAddr *net.UDPAddr
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func (self *IntrinsicRelayer) startLocalHTTPProxy() error {
}

func (self *IntrinsicRelayer) startLocalUDPServer() error {
laddr, err := net.ResolveUDPAddr("udp", self.Local)
laddr, err := net.ResolveUDPAddr("udp", self.LocalUDP)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,8 +91,12 @@ func (self *IntrinsicRelayer) startLocalUDPServer() error {
return nil
}

func (self *IntrinsicRelayer) IsEndPoint() bool {
return len(self.Next) == 0
}

func (self *IntrinsicRelayer) Run() {
if len(self.Next) != 0 && !self.NoUDP {
if !self.IsEndPoint() && self.LocalUDP != "" {
if err := self.startLocalUDPServer(); err != nil {
log.Println(err)
return
Expand All @@ -105,7 +109,7 @@ func (self *IntrinsicRelayer) Run() {
}
defer ln.Close()
// self.LocalHTTPProxy relies on socks proxy.
if len(self.Next) != 0 && self.LocalHTTPProxy != "" {
if !self.IsEndPoint() && self.LocalHTTPProxy != "" {
if err := self.startLocalHTTPProxy(); err != nil {
log.Println(err)
return
Expand All @@ -117,7 +121,7 @@ func (self *IntrinsicRelayer) Run() {
log.Println(err)
break
}
if len(self.Next) == 0 {
if self.IsEndPoint() {
go self.ServeAsEndRelayer(c)
} else {
go self.ServeAsLocalRelayer(c)
Expand Down

0 comments on commit 436a39b

Please sign in to comment.