github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/reality/reality.go (about) 1 package reality 2 3 import ( 4 "context" 5 "encoding/base64" 6 "encoding/hex" 7 "fmt" 8 9 "github.com/Asutorufa/yuhaiin/pkg/net/dialer" 10 "github.com/Asutorufa/yuhaiin/pkg/net/netapi" 11 "github.com/Asutorufa/yuhaiin/pkg/protos/config/listener" 12 "github.com/xtls/reality" 13 ) 14 15 /* 16 Private key: CKr8-tipwbEwwDa97S3Rwqzs9L8AlcLOCZJah1zjLlw 17 Public key: SOW7P-17ibm_-kz-QUQwGGyitSbsa5wOmRGAigGvDH8 18 */ 19 20 func ShortIDMap(s *listener.Transport_Reality) (map[[8]byte]bool, error) { 21 maps := make(map[[8]byte]bool, len(s.Reality.ShortId)) 22 23 for _, v := range s.Reality.ShortId { 24 var id [8]byte 25 length, err := hex.Decode(id[:], []byte(v)) 26 if err != nil { 27 return nil, fmt.Errorf("decode hex failed: %w", err) 28 } 29 30 if length > 8 { 31 return nil, fmt.Errorf("short id length is large than 8") 32 } 33 34 maps[id] = true 35 } 36 37 return maps, nil 38 } 39 40 func ServerNameMap(s *listener.Transport_Reality) map[string]bool { 41 maps := make(map[string]bool, len(s.Reality.ServerName)) 42 43 for _, v := range s.Reality.ServerName { 44 maps[v] = true 45 } 46 47 return maps 48 } 49 50 func NewServer(config *listener.Transport_Reality) func(netapi.Listener) (netapi.Listener, error) { 51 privateKey, err := base64.RawURLEncoding.DecodeString(config.Reality.PrivateKey) 52 if err != nil { 53 return listener.ErrorTransportFunc(err) 54 } 55 56 ids, err := ShortIDMap(config) 57 if err != nil { 58 return listener.ErrorTransportFunc(err) 59 } 60 61 return func(ii netapi.Listener) (netapi.Listener, error) { 62 lis, err := ii.Stream(context.TODO()) 63 if err != nil { 64 return nil, err 65 } 66 67 config := &reality.Config{ 68 DialContext: dialer.DialContext, 69 Show: config.Reality.Debug, 70 Type: "tcp", 71 ShortIds: ids, 72 ServerNames: ServerNameMap(config), 73 Dest: config.Reality.Dest, 74 PrivateKey: privateKey, 75 SessionTicketsDisabled: true, 76 } 77 78 lis = reality.NewListener(lis, config) 79 80 return netapi.PatchStream(lis, ii), nil 81 82 } 83 } 84 85 func init() { 86 listener.RegisterTransport(NewServer) 87 }