Refactor socksaddr

This commit is contained in:
世界 2022-05-04 19:12:27 +08:00
parent 9378ae739c
commit b35c53ca8f
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
54 changed files with 1191 additions and 666 deletions

View file

@ -1,3 +1,5 @@
//go:build linux
package main
import (

View file

@ -18,9 +18,8 @@ import (
E "github.com/sagernet/sing/common/exceptions"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/protocol/trojan"
transTLS "github.com/sagernet/sing/transport/tls"
"github.com/sirupsen/logrus"
@ -177,14 +176,14 @@ func (i *TrojanInstance) NewConnection(ctx context.Context, conn net.Conn, metad
userCtx := ctx.(*trojan.Context[int])
conn = i.user.TrackConnection(userCtx.User, conn)
logrus.Info(i.id, ": user ", userCtx.User, " TCP ", metadata.Source, " ==> ", metadata.Destination)
destConn, err := network.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
destConn, err := N.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
if err != nil {
return err
}
return rw.CopyConn(ctx, conn, destConn)
}
func (i *TrojanInstance) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (i *TrojanInstance) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
userCtx := ctx.(*trojan.Context[int])
conn = i.user.TrackPacketConnection(userCtx.User, conn)
logrus.Info(i.id, ": user ", userCtx.User, " UDP ", metadata.Source, " ==> ", metadata.Destination)
@ -192,7 +191,7 @@ func (i *TrojanInstance) NewPacketConnection(ctx context.Context, conn socks.Pac
if err != nil {
return err
}
return socks.CopyNetPacketConn(ctx, conn, udpConn)
return N.CopyNetPacketConn(ctx, conn, udpConn)
}
func (i *TrojanInstance) loopRequests() {
@ -205,7 +204,7 @@ func (i *TrojanInstance) loopRequests() {
go func() {
hErr := i.service.NewConnection(context.Background(), conn, M.Metadata{
Protocol: "tls",
Source: M.AddrPortFromNetAddr(conn.RemoteAddr()),
Source: M.SocksaddrFromNet(conn.RemoteAddr()),
})
if hErr != nil {
i.HandleError(hErr)

View file

@ -8,7 +8,7 @@ import (
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/protocol/socks"
N "github.com/sagernet/sing/common/network"
)
type UserManager struct {
@ -40,7 +40,7 @@ func (m *UserManager) TrackConnection(userId int, conn net.Conn) net.Conn {
return &TrackConn{conn, user}
}
func (m *UserManager) TrackPacketConnection(userId int, conn socks.PacketConn) socks.PacketConn {
func (m *UserManager) TrackPacketConnection(userId int, conn N.PacketConn) N.PacketConn {
m.access.Lock()
defer m.access.Unlock()
var user *User
@ -112,11 +112,11 @@ func (c *TrackConn) ReadFrom(r io.Reader) (n int64, err error) {
}
type TrackPacketConn struct {
socks.PacketConn
N.PacketConn
*User
}
func (c *TrackPacketConn) ReadPacket(buffer *buf.Buffer) (*M.AddrPort, error) {
func (c *TrackPacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
destination, err := c.PacketConn.ReadPacket(buffer)
if err == nil {
atomic.AddUint64(&c.Upload, uint64(buffer.Len()))
@ -124,7 +124,7 @@ func (c *TrackPacketConn) ReadPacket(buffer *buf.Buffer) (*M.AddrPort, error) {
return destination, err
}
func (c *TrackPacketConn) WritePacket(buffer *buf.Buffer, destination *M.AddrPort) error {
func (c *TrackPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
n := buffer.Len()
err := c.PacketConn.WritePacket(buffer, destination)
if err == nil {

View file

@ -11,7 +11,7 @@ import (
"github.com/sagernet/sing/common/buf"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/protocol/socks5"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/dns/dnsmessage"
@ -29,11 +29,8 @@ func main() {
}
func run(cmd *cobra.Command, args []string) {
server, err := M.ParseAddress(args[0])
if err != nil {
logrus.Fatal("invalid server address ", args[0])
}
err = testSocksTCP(server)
server := M.ParseSocksaddr(args[0])
err := testSocksTCP(server)
if err != nil {
logrus.Fatal(err)
}
@ -43,16 +40,16 @@ func run(cmd *cobra.Command, args []string) {
}
}
func testSocksTCP(server *M.AddrPort) error {
func testSocksTCP(server M.Socksaddr) error {
tcpConn, err := net.Dial("tcp", server.String())
if err != nil {
return err
}
response, err := socks.ClientHandshake(tcpConn, socks.Version5, socks.CommandConnect, M.AddrPortFrom(M.ParseAddr("1.0.0.1"), 53), "", "")
response, err := socks5.ClientHandshake(tcpConn, socks5.Version5, socks5.CommandConnect, M.ParseSocksaddrHostPort("1.0.0.1", "53"), "", "")
if err != nil {
return err
}
if response.ReplyCode != socks.ReplyCodeSuccess {
if response.ReplyCode != socks5.ReplyCodeSuccess {
logrus.Fatal("socks tcp handshake failure: ", response.ReplyCode)
}
@ -98,17 +95,17 @@ func testSocksTCP(server *M.AddrPort) error {
return nil
}
func testSocksUDP(server *M.AddrPort) error {
func testSocksUDP(server M.Socksaddr) error {
tcpConn, err := net.Dial("tcp", server.String())
if err != nil {
return err
}
dest := M.AddrPortFrom(M.ParseAddr("1.0.0.1"), 53)
response, err := socks.ClientHandshake(tcpConn, socks.Version5, socks.CommandUDPAssociate, dest, "", "")
dest := M.ParseSocksaddrHostPort("1.0.0.1", "53")
response, err := socks5.ClientHandshake(tcpConn, socks5.Version5, socks5.CommandUDPAssociate, dest, "", "")
if err != nil {
return err
}
if response.ReplyCode != socks.ReplyCodeSuccess {
if response.ReplyCode != socks5.ReplyCodeSuccess {
logrus.Fatal("socks tcp handshake failure: ", response.ReplyCode)
}
var dialer net.Dialer
@ -116,7 +113,7 @@ func testSocksUDP(server *M.AddrPort) error {
if err != nil {
return err
}
assConn := socks.NewAssociateConn(tcpConn, udpConn, dest)
assConn := socks5.NewAssociateConn(tcpConn, udpConn, dest)
message := &dnsmessage.Message{}
message.Header.ID = 1
message.Header.RecursionDesired = true

View file

@ -23,6 +23,7 @@ import (
"github.com/sagernet/sing/common/geosite"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/random"
"github.com/sagernet/sing/common/redir"
"github.com/sagernet/sing/common/rw"
@ -30,7 +31,6 @@ import (
"github.com/sagernet/sing/protocol/shadowsocks"
"github.com/sagernet/sing/protocol/shadowsocks/shadowaead"
"github.com/sagernet/sing/protocol/shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/transport/mixed"
"github.com/sagernet/sing/transport/system"
"github.com/sirupsen/logrus"
@ -101,7 +101,7 @@ Only available with Linux kernel > 3.7.0.`)
type client struct {
*mixed.Listener
*geosite.Matcher
server *M.AddrPort
server M.Socksaddr
method shadowsocks.Method
dialer net.Dialer
bypass string
@ -163,7 +163,7 @@ func newClient(f *flags) (*client, error) {
}
c := &client{
server: M.AddrPortFrom(M.ParseAddr(f.Server), f.ServerPort),
server: M.SocksaddrFromAddrPort(M.ParseAddr(f.Server), f.ServerPort),
bypass: f.Bypass,
}
@ -294,7 +294,7 @@ func newClient(f *flags) (*client, error) {
return c, nil
}
func bypass(conn net.Conn, destination *M.AddrPort) error {
func bypass(conn net.Conn, destination M.Socksaddr) error {
logrus.Info("BYPASS ", conn.RemoteAddr(), " ==> ", destination)
serverConn, err := net.Dial("tcp", destination.String())
if err != nil {
@ -313,12 +313,12 @@ func bypass(conn net.Conn, destination *M.AddrPort) error {
func (c *client) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
if c.bypass != "" {
if metadata.Destination.Addr.Family().IsFqdn() {
if c.Match(metadata.Destination.Addr.Fqdn()) {
if metadata.Destination.Family().IsFqdn() {
if c.Match(metadata.Destination.Fqdn) {
return bypass(conn, metadata.Destination)
}
} else {
if geoip.Match(c.bypass, metadata.Destination.Addr.Addr().AsSlice()) {
if geoip.Match(c.bypass, metadata.Destination.Addr.AsSlice()) {
return bypass(conn, metadata.Destination)
}
}
@ -354,14 +354,14 @@ func (c *client) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
return rw.CopyConn(ctx, serverConn, conn)
}
func (c *client) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (c *client) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
logrus.Info("outbound ", metadata.Protocol, " UDP ", metadata.Source, " ==> ", metadata.Destination)
udpConn, err := c.dialer.DialContext(ctx, "udp", c.server.String())
if err != nil {
return err
}
serverConn := c.method.DialPacketConn(udpConn)
return socks.CopyPacketConn(ctx, serverConn, conn)
return N.CopyPacketConn(ctx, serverConn, conn)
}
func run(cmd *cobra.Command, flags *flags) {

View file

@ -17,13 +17,12 @@ import (
E "github.com/sagernet/sing/common/exceptions"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/random"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/protocol/shadowsocks"
"github.com/sagernet/sing/protocol/shadowsocks/shadowaead"
"github.com/sagernet/sing/protocol/shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/transport/tcp"
"github.com/sagernet/sing/transport/udp"
"github.com/sirupsen/logrus"
@ -191,23 +190,23 @@ func (s *server) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
return s.service.NewConnection(ctx, conn, metadata)
}
logrus.Info("inbound TCP ", conn.RemoteAddr(), " ==> ", metadata.Destination)
destConn, err := network.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
destConn, err := N.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
if err != nil {
return err
}
return rw.CopyConn(ctx, conn, destConn)
}
func (s *server) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (s *server) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
logrus.Info("inbound UDP ", metadata.Source, " ==> ", metadata.Destination)
udpConn, err := net.ListenUDP("udp", nil)
if err != nil {
return err
}
return socks.CopyNetPacketConn(ctx, conn, udpConn)
return N.CopyNetPacketConn(ctx, conn, udpConn)
}
func (s *server) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *server) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
logrus.Trace("inbound raw UDP from ", metadata.Source)
return s.service.NewPacket(conn, buffer, metadata)
}

View file

@ -19,9 +19,9 @@ import (
E "github.com/sagernet/sing/common/exceptions"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/redir"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/protocol/trojan"
"github.com/sagernet/sing/transport/mixed"
"github.com/sirupsen/logrus"
@ -148,7 +148,7 @@ func newClient(f *flags) (*client, error) {
}
c := &client{
server: M.AddrPortFrom(M.ParseAddr(f.Server), f.ServerPort).String(),
server: netip.AddrPortFrom(M.ParseAddr(f.Server), f.ServerPort).String(),
key: trojan.Key(f.Password),
sni: f.ServerName,
insecure: f.Insecure,
@ -319,7 +319,7 @@ func (c *client) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
return rw.CopyConn(ctx, clientConn, conn)
}
func (c *client) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (c *client) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
logrus.Info("outbound ", metadata.Protocol, " UDP ", metadata.Source, " ==> ", metadata.Destination)
tlsConn, err := c.connect(ctx)
@ -332,7 +332,7 @@ func (c *client) NewPacketConnection(ctx context.Context, conn socks.PacketConn,
}
return socks.CopyPacketConn(ctx, &trojan.PacketConn{Conn: tlsConn}, conn)*/
clientConn := trojan.NewClientPacketConn(tlsConn, c.key)
return socks.CopyPacketConn(ctx, clientConn, conn)
return N.CopyPacketConn(ctx, clientConn, conn)
}
func (c *client) HandleError(err error) {

View file

@ -17,9 +17,8 @@ import (
E "github.com/sagernet/sing/common/exceptions"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/network"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/protocol/trojan"
"github.com/sagernet/sing/transport/tcp"
transTLS "github.com/sagernet/sing/transport/tls"
@ -193,7 +192,7 @@ func (s *server) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
}
return s.service.NewConnection(ctx, tls.Server(conn, &s.tlsConfig), metadata)
}
destConn, err := network.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
destConn, err := N.SystemDialer.DialContext(context.Background(), "tcp", metadata.Destination)
if err != nil {
return err
}
@ -201,13 +200,13 @@ func (s *server) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
return rw.CopyConn(ctx, conn, destConn)
}
func (s *server) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (s *server) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
logrus.Info("inbound UDP ", metadata.Source, " ==> ", metadata.Destination)
udpConn, err := net.ListenUDP("udp", nil)
if err != nil {
return err
}
return socks.CopyNetPacketConn(ctx, conn, udpConn)
return N.CopyNetPacketConn(ctx, conn, udpConn)
}
func (s *server) HandleError(err error) {

View file

@ -13,10 +13,11 @@ import (
E "github.com/sagernet/sing/common/exceptions"
_ "github.com/sagernet/sing/common/log"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/redir"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/common/uot"
"github.com/sagernet/sing/protocol/socks"
"github.com/sagernet/sing/protocol/socks5"
"github.com/sagernet/sing/transport/mixed"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -102,7 +103,7 @@ func (c *localClient) NewConnection(ctx context.Context, conn net.Conn, metadata
return E.Cause(err, "connect to upstream")
}
_, err = socks.ClientHandshake(upstream, socks.Version5, socks.CommandConnect, metadata.Destination, "", "")
_, err = socks5.ClientHandshake(upstream, socks5.Version5, socks5.CommandConnect, metadata.Destination, "", "")
if err != nil {
return E.Cause(err, "upstream handshake failed")
}
@ -110,19 +111,19 @@ func (c *localClient) NewConnection(ctx context.Context, conn net.Conn, metadata
return rw.CopyConn(context.Background(), upstream, conn)
}
func (c *localClient) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
func (c *localClient) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
upstream, err := net.Dial("tcp", c.upstream)
if err != nil {
return E.Cause(err, "connect to upstream")
}
_, err = socks.ClientHandshake(upstream, socks.Version5, socks.CommandConnect, M.AddrPortFrom(M.AddrFromFqdn(uot.UOTMagicAddress), 443), "", "")
_, err = socks5.ClientHandshake(upstream, socks5.Version5, socks5.CommandConnect, M.ParseSocksaddrHostPort(uot.UOTMagicAddress, "443"), "", "")
if err != nil {
return E.Cause(err, "upstream handshake failed")
}
client := uot.NewClientConn(upstream)
return socks.CopyPacketConn(ctx, client, conn)
return N.CopyPacketConn(ctx, client, conn)
}
func (c *localClient) OnError(err error) {