MCPcopy
hub / github.com/XTLS/REALITY / Server

Function Server

tls.go:162–485  ·  view source on GitHub ↗

You MUST call `DetectPostHandshakeRecordsLens(config)` in advance manually if you don't use REALITY's listener, e.g., Xray-core's RAW transport.

(ctx context.Context, conn net.Conn, config *Config)

Source from the content-addressed store, hash-verified

160// You MUST call `DetectPostHandshakeRecordsLens(config)` in advance manually
161// if you don't use REALITY's listener, e.g., Xray-core's RAW transport.
162func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
163 remoteAddr := conn.RemoteAddr().String()
164 if config.Show {
165 fmt.Printf("REALITY remoteAddr: %v\n", remoteAddr)
166 }
167
168 target, err := config.DialContext(ctx, config.Type, config.Dest)
169 if err != nil {
170 conn.Close()
171 return nil, errors.New("REALITY: failed to dial dest: " + err.Error())
172 }
173
174 if config.Xver == 1 || config.Xver == 2 {
175 if _, err = proxyproto.HeaderProxyFromAddrs(config.Xver, conn.RemoteAddr(), conn.LocalAddr()).WriteTo(target); err != nil {
176 target.Close()
177 conn.Close()
178 return nil, errors.New("REALITY: failed to send PROXY protocol: " + err.Error())
179 }
180 }
181
182 raw := conn
183 if pc, ok := conn.(*proxyproto.Conn); ok {
184 raw = pc.Raw() // for TCP splicing in io.Copy()
185 }
186 underlying := raw.(CloseWriteConn) // *net.TCPConn or *net.UnixConn
187
188 mutex := new(sync.Mutex)
189
190 hs := serverHandshakeStateTLS13{
191 c: &Conn{
192 conn: &MirrorConn{
193 Mutex: mutex,
194 Conn: conn,
195 Target: target,
196 },
197 config: config,
198 },
199 ctx: context.Background(),
200 }
201
202 copying := false
203
204 waitGroup := new(sync.WaitGroup)
205 waitGroup.Add(2)
206
207 go func() {
208 for {
209 mutex.Lock()
210 hs.clientHello, _, err = hs.c.readClientHello(context.Background()) // TODO: Change some rules in this function.
211 if copying || err != nil || hs.c.vers != VersionTLS13 || !config.ServerNames[hs.clientHello.serverName] {
212 break
213 }
214 var peerPub []byte
215 for _, keyShare := range hs.clientHello.keyShares {
216 if keyShare.group == X25519 && len(keyShare.data) == 32 {
217 peerPub = keyShare.data
218 break
219 }

Callers 2

QUICServerFunction · 0.85
NewListenerFunction · 0.85

Calls 15

handshakeMethod · 0.95
readClientFinishedMethod · 0.95
ValueFunction · 0.85
NewRatelimitedConnFunction · 0.85
recordTypeTypeAlias · 0.85
cipherSuiteTLS13ByIDFunction · 0.85
RemoteAddrMethod · 0.80
DialContextMethod · 0.80
LocalAddrMethod · 0.80
readClientHelloMethod · 0.80
incSeqMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…