Fix parsing addr

This commit is contained in:
世界 2022-05-11 20:09:29 +08:00
parent 45896ae10f
commit e44d440031
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 34 additions and 15 deletions

View file

@ -2,14 +2,15 @@ package main
import (
"archive/tar"
"github.com/klauspost/compress/zip"
"github.com/sagernet/sing/common/log"
"github.com/spf13/cobra"
"github.com/ulikunitz/xz"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/klauspost/compress/zip"
"github.com/sagernet/sing/common/log"
"github.com/spf13/cobra"
"github.com/ulikunitz/xz"
)
var logger = log.NewLogger("buildx")
@ -120,7 +121,7 @@ func buildOne(app string, appPath string, outputDir string, release bool) error
if err != nil {
return err
}
err = os.MkdirAll("bin", 0755)
err = os.MkdirAll("bin", 0o755)
if err != nil {
return err
}

View file

@ -55,7 +55,7 @@ func run(cmd *cobra.Command, args []string) {
}
func testSocksTCP(client *socks.Client) error {
tcpConn, err := client.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("1.0.0.1", "53"))
tcpConn, err := client.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("1.0.0.1", 53))
if err != nil {
return err
}
@ -103,7 +103,7 @@ func testSocksTCP(client *socks.Client) error {
}
func testSocksUDP(client *socks.Client) error {
udpConn, err := client.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("1.0.0.1", "53"))
udpConn, err := client.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("1.0.0.1", 53))
if err != nil {
return err
}

View file

@ -166,7 +166,7 @@ func newClient(f *flags) (*client, error) {
}
c := &client{
server: M.SocksaddrFromAddrPort(M.ParseAddr(f.Server), f.ServerPort),
server: M.ParseSocksaddrHostPort(f.Server, f.ServerPort),
bypass: f.Bypass,
}

View file

@ -94,7 +94,7 @@ func run(cmd *cobra.Command, f *flags) {
type client struct {
*mixed.Listener
server string
server M.Socksaddr
key [trojan.KeyLength]byte
sni string
insecure bool
@ -149,7 +149,7 @@ func newClient(f *flags) (*client, error) {
}
c := &client{
server: netip.AddrPortFrom(M.ParseAddr(f.Server), f.ServerPort).String(),
server: M.ParseSocksaddrHostPort(f.Server, f.ServerPort),
key: trojan.Key(f.Password),
sni: f.ServerName,
insecure: f.Insecure,
@ -174,7 +174,7 @@ func newClient(f *flags) (*client, error) {
}
func (c *client) connect(ctx context.Context) (*cTLS.Conn, error) {
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.server)
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.server.String())
if err != nil {
return nil, err
}
@ -187,7 +187,7 @@ func (c *client) connect(ctx context.Context) (*cTLS.Conn, error) {
}
func (c *client) connectUTLS(ctx context.Context) (*tls.UConn, error) {
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.server)
tcpConn, err := c.dialer.DialContext(ctx, "tcp", c.server.String())
if err != nil {
return nil, err
}

View file

@ -181,10 +181,28 @@ func ParseSocksaddr(address string) Socksaddr {
if err != nil {
return Socksaddr{}
}
return ParseSocksaddrHostPort(host, port)
return ParseSocksaddrHostPortStr(host, port)
}
func ParseSocksaddrHostPort(host string, portStr string) Socksaddr {
func ParseSocksaddrHostPort(host string, port uint16) Socksaddr {
netAddr, err := netip.ParseAddr(host)
if netAddr.Is4In6() {
netAddr = netip.AddrFrom4(netAddr.As4())
}
if err != nil {
return Socksaddr{
Fqdn: host,
Port: port,
}
} else {
return Socksaddr{
Addr: netAddr,
Port: port,
}
}
}
func ParseSocksaddrHostPortStr(host string, portStr string) Socksaddr {
port, _ := strconv.Atoi(portStr)
netAddr, err := netip.ParseAddr(host)
if netAddr.Is4In6() {

View file

@ -45,7 +45,7 @@ func HandleRequest(ctx context.Context, request *http.Request, conn net.Conn, au
if portStr == "" {
portStr = "80"
}
destination := M.ParseSocksaddrHostPort(request.URL.Hostname(), portStr)
destination := M.ParseSocksaddrHostPortStr(request.URL.Hostname(), portStr)
_, err := fmt.Fprintf(conn, "HTTP/%d.%d %03d %s\r\n\r\n", request.ProtoMajor, request.ProtoMinor, http.StatusOK, "Connection established")
if err != nil {
return E.Cause(err, "write http response")