@@ -3,14 +3,10 @@ package outbound
3
3
import (
4
4
"context"
5
5
"crypto/tls"
6
- "encoding/binary"
7
- "errors"
8
6
"fmt"
9
- "io"
10
7
"net"
11
8
"net/http"
12
9
"strconv"
13
- "sync"
14
10
15
11
"github.com/metacubex/mihomo/common/convert"
16
12
N "github.com/metacubex/mihomo/common/net"
@@ -30,11 +26,6 @@ import (
30
26
M "github.com/metacubex/sing/common/metadata"
31
27
)
32
28
33
- const (
34
- // max packet length
35
- maxLength = 1024 << 3
36
- )
37
-
38
29
type Vless struct {
39
30
* Base
40
31
client * vless.Client
@@ -188,9 +179,6 @@ func (v *Vless) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
188
179
}
189
180
}
190
181
conn , err = v .client .StreamConn (c , parseVlessAddr (metadata , v .option .XUDP ))
191
- if v .option .PacketAddr {
192
- conn = packetaddr .NewBindConn (conn )
193
- }
194
182
} else {
195
183
conn , err = v .client .StreamConn (c , parseVlessAddr (metadata , false ))
196
184
}
@@ -352,12 +340,11 @@ func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metada
352
340
), v ), nil
353
341
} else if v .option .PacketAddr {
354
342
return newPacketConn (N .NewThreadSafePacketConn (
355
- packetaddr .NewConn (& vlessPacketConn {
356
- Conn : c , rAddr : metadata .UDPAddr (),
357
- }, M .SocksaddrFromNet (metadata .UDPAddr ())),
343
+ packetaddr .NewConn (v .client .PacketConn (c , metadata .UDPAddr ()),
344
+ M .SocksaddrFromNet (metadata .UDPAddr ())),
358
345
), v ), nil
359
346
}
360
- return newPacketConn (N .NewThreadSafePacketConn (& vlessPacketConn { Conn : c , rAddr : metadata .UDPAddr ()} ), v ), nil
347
+ return newPacketConn (N .NewThreadSafePacketConn (v . client . PacketConn ( c , metadata .UDPAddr ()) ), v ), nil
361
348
}
362
349
363
350
// SupportUOT implements C.ProxyAdapter
@@ -408,98 +395,6 @@ func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr {
408
395
}
409
396
}
410
397
411
- type vlessPacketConn struct {
412
- net.Conn
413
- rAddr net.Addr
414
- remain int
415
- mux sync.Mutex
416
- cache [2 ]byte
417
- }
418
-
419
- func (c * vlessPacketConn ) writePacket (payload []byte ) (int , error ) {
420
- binary .BigEndian .PutUint16 (c .cache [:], uint16 (len (payload )))
421
-
422
- if _ , err := c .Conn .Write (c .cache [:]); err != nil {
423
- return 0 , err
424
- }
425
-
426
- return c .Conn .Write (payload )
427
- }
428
-
429
- func (c * vlessPacketConn ) WriteTo (b []byte , addr net.Addr ) (int , error ) {
430
- total := len (b )
431
- if total == 0 {
432
- return 0 , nil
433
- }
434
-
435
- if total <= maxLength {
436
- return c .writePacket (b )
437
- }
438
-
439
- offset := 0
440
-
441
- for offset < total {
442
- cursor := offset + maxLength
443
- if cursor > total {
444
- cursor = total
445
- }
446
-
447
- n , err := c .writePacket (b [offset :cursor ])
448
- if err != nil {
449
- return offset + n , err
450
- }
451
-
452
- offset = cursor
453
- if offset == total {
454
- break
455
- }
456
- }
457
-
458
- return total , nil
459
- }
460
-
461
- func (c * vlessPacketConn ) ReadFrom (b []byte ) (int , net.Addr , error ) {
462
- c .mux .Lock ()
463
- defer c .mux .Unlock ()
464
-
465
- if c .remain > 0 {
466
- length := len (b )
467
- if c .remain < length {
468
- length = c .remain
469
- }
470
-
471
- n , err := c .Conn .Read (b [:length ])
472
- if err != nil {
473
- return 0 , c .rAddr , err
474
- }
475
-
476
- c .remain -= n
477
- return n , c .rAddr , nil
478
- }
479
-
480
- if _ , err := c .Conn .Read (b [:2 ]); err != nil {
481
- return 0 , c .rAddr , err
482
- }
483
-
484
- total := int (binary .BigEndian .Uint16 (b [:2 ]))
485
- if total == 0 {
486
- return 0 , c .rAddr , nil
487
- }
488
-
489
- length := len (b )
490
- if length > total {
491
- length = total
492
- }
493
-
494
- if _ , err := io .ReadFull (c .Conn , b [:length ]); err != nil {
495
- return 0 , c .rAddr , errors .New ("read packet error" )
496
- }
497
-
498
- c .remain = total - length
499
-
500
- return length , c .rAddr , nil
501
- }
502
-
503
398
func NewVless (option VlessOption ) (* Vless , error ) {
504
399
var addons * vless.Addons
505
400
if option .Network != "ws" && len (option .Flow ) >= 16 {
0 commit comments