将3d80f6a的为smux实现user分流的方式推广到udp和vless/trojan

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 54419e9515
commit 8ab14efb16
5 changed files with 55 additions and 3 deletions

10
main.go
View File

@@ -605,6 +605,14 @@ func handshakeInserver(iics *incomingInserverConnState) (wlc net.Conn, udp_wlc n
}
}
}
} else if udp_wlc != nil {
if u, ok := udp_wlc.(utils.User); ok {
if udp_wlc1 != nil {
if us, ok := udp_wlc1.(utils.UserAssigner); ok {
us.SetUser(u)
}
}
}
}
passToOutClient(newiics, false, wlc1, udp_wlc1, targetAddr1)
@@ -1029,6 +1037,8 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
}
if uc, ok := wlc.(utils.User); ok {
desc.UserIdentityStr = uc.IdentityStr()
} else if uc, ok := udp_wlc.(utils.User); ok {
desc.UserIdentityStr = uc.IdentityStr()
}
if ce := iics.CanLogDebug("Try routing"); ce != nil {

View File

@@ -25,6 +25,14 @@ type MuxMarkerConn struct {
func (mh *MuxMarkerConn) IsMux() bool { return true }
// 实现 utils.MuxMarker, utils.User
type UserMuxMarker struct {
utils.User
netLayer.ReadWrapper
}
func (mh *UserMuxMarker) IsMux() bool { return true }
// some client may 建立tcp连接后首先由客户端读服务端的数据虽较少见但确实存在.
// Anyway firstpayload might not be read, and we should try to reduce this delay.
// 也有可能是有人用 nc 来测试,也会遇到这种读不到 firstpayload 的情况

View File

@@ -17,6 +17,8 @@ type UDPConn struct {
bufr *bufio.Reader
handshakeBuf *bytes.Buffer
fullcone bool
upstreamUser utils.User
}
func NewUDPConn(conn net.Conn, optionalReader io.Reader) (uc *UDPConn) {
@@ -31,6 +33,37 @@ func NewUDPConn(conn net.Conn, optionalReader io.Reader) (uc *UDPConn) {
return
}
func (c *UDPConn) SetUser(u utils.User) {
c.upstreamUser = u
}
func (c *UDPConn) IdentityStr() string {
if c.upstreamUser != nil {
return c.upstreamUser.IdentityStr()
}
return ""
}
func (c *UDPConn) IdentityBytes() []byte {
if c.upstreamUser != nil {
return c.upstreamUser.IdentityBytes()
}
return nil
}
func (c *UDPConn) AuthStr() string {
if c.upstreamUser != nil {
return c.upstreamUser.AuthStr()
}
return ""
}
func (c *UDPConn) AuthBytes() []byte {
if c.upstreamUser != nil {
return c.upstreamUser.AuthBytes()
}
return nil
}
func (u *UDPConn) Fullcone() bool {
return u.fullcone
}
@@ -43,7 +76,6 @@ func (u *UDPConn) ReadMsg() ([]byte, netLayer.Addr, error) {
// https://github.com/p4gefau1t/trojan-go/blob/2dc60f52e79ff8b910e78e444f1e80678e936450/tunnel/simplesocks/conn.go#L41
// https://github.com/p4gefau1t/trojan-go/blob/2dc60f52e79ff8b910e78e444f1e80678e936450/tunnel/trojan/packet.go#L34
//可以看到和trojan协议一样长度后面要跟随 crlf
//主要是本以为simplesocks能更加simple的去掉crlf结果还是差强人意。。。
addr, err := GetAddrFrom(u.bufr)
if err != nil {

View File

@@ -220,7 +220,8 @@ realPart:
}
if ismux {
mh := &proxy.MuxMarkerConn{
mh := &proxy.UserMuxMarker{
User: theUser.(User),
ReadWrapper: netLayer.ReadWrapper{
Conn: underlay,
},

View File

@@ -257,7 +257,8 @@ realPart:
}
if ismux {
mm := &proxy.MuxMarkerConn{
mm := &proxy.UserMuxMarker{
User: utils.V2rayUser(thisUUIDBytes),
ReadWrapper: netLayer.ReadWrapper{
Conn: underlay,
},