Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouzierinverse committed Dec 19, 2024
1 parent 86a94d3 commit aba0054
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions go/chisel/share/settings/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func DecodeRemote(s string) (*Remote, error) {

if r.Reverse && r.LocalPort == "0" {
if err := r.setupLocalPort(); err != nil {
return nil, errors.New("Cannot bind to a local port")
return nil, fmt.Errorf("Cannot bind to a local port: %w", err)
}
}

Expand All @@ -161,14 +161,14 @@ func DecodeRemote(s string) (*Remote, error) {

func (r *Remote) setupLocalPort() error {
if r.LocalProto == "tcp" {
addr, err := net.ResolveTCPAddr("tcp", r.LocalHost+":"+r.LocalPort)
addr, err := net.ResolveTCPAddr("tcp", r.Local())
if err != nil {
return fmt.Errorf("resolve: %w", err)
}

tl, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
return fmt.Errorf("net.ListenTCP: %w", err)
}

r.LocalPort = strconv.Itoa(tl.Addr().(*net.TCPAddr).Port)
Expand All @@ -180,18 +180,20 @@ func (r *Remote) setupLocalPort() error {
if r.LocalProto == "udp" {
addr, err := net.ResolveUDPAddr("udp", r.Local())
if err != nil {
return err
return fmt.Errorf("resolve: %w", err)
}

conn, err := net.ListenUDP("udp", addr)
if err != nil {
return err
return fmt.Errorf("net.ListenUDP: %w", err)
}

r.LocalPort = strconv.Itoa(conn.LocalAddr().(*net.UDPAddr).Port)
r.ReusedUdpConn = conn
r.Dynamic = true
return nil
}

return errors.New("Proto not supported")
}

Expand Down

0 comments on commit aba0054

Please sign in to comment.