File size: 21,153 Bytes
557c1b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
package main

import (
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"
)

type Config struct {
	UUID         string
	XPath        string
	SubPath      string
	Domain       string
	Name         string
	Port         string
	LogLevel     string
	ChunkSize    int
	NezhaServer  string
	NezhaPort    string
	NezhaKey     string
	AutoAccess   bool
}

type VlessSession struct {
	UUID           string
	Remote         net.Conn
	ResponseHeader []byte
	mu             sync.Mutex
}

type CloudflareSpeedMeta struct {
	Country        string `json:"country"`
	AsOrganization string `json:"asOrganization"`
}

var (
	config       *Config
	sessions     = make(map[string]*VlessSession)
	sessionsMu   sync.RWMutex
	logger       *log.Logger
)

func init() {
	logger = log.New(os.Stdout, "[Server] ", log.LstdFlags)
}

// load config
func loadConfig() *Config {
	uuid := getEnv("UUID", "a2056d0d-c98e-4aeb-9aab-37f64edd5710")  // UUID,哪吒v1依赖UUID
	nezhaServer := getEnv("NEZHA_SERVER", "")    // 哪吒v1形式:nezha.xxx.com:8008  哪吒v0形式:nezha.xxx.com
	nezhaPort := getEnv("NEZHA_PORT", "")        // 哪吒v1请留空,哪吒v0的agent端口
	nezhaKey := getEnv("NEZHA_KEY", "")          // 哪吒v1的NZ_CLIENT_SECRET或哪吒v0的agent密钥
	subPath := getEnv("SUB_PATH", "sub")         // 节点订阅token
	name := getEnv("NAME", "Xhttp")              // 节点名称
	port := getEnv("PORT", "3000")               // 监听端口
	domain := getEnv("DOMAIN", "sap-ryanrsacks-jp.2.2.a.a.f.f.0.7.0.0.6.2.ip6.arpa")               // 服务域名

	xpath := getEnv("XPATH", uuid[:8])
	autoAccess := false
	if getEnv("AUTO_ACCESS", "false") == "true" {  // 是否开启自动保活,true开启,false关闭,默认关闭
		autoAccess = true
	}
	
	return &Config{
		UUID:        uuid,
		XPath:       xpath,
		NezhaServer: nezhaServer,
		NezhaPort:   nezhaPort,
		NezhaKey:    nezhaKey,
		SubPath:     subPath,
		Domain:      domain,
		Name:        name,
		Port:        port,
		ChunkSize:   32768,
		AutoAccess:  autoAccess,
		LogLevel:    getEnv("LOG_LEVEL", "none"), // 日志等级 none, info, debug, warn, error
	}
}

func getEnv(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}

// Log function
func logf(level, format string, args ...interface{}) {
	levels := map[string]int{"none": -1, "debug": 0, "info": 1, "warn": 2, "error": 3}
	
	configLevel := levels[config.LogLevel]
	messageLevel := levels[level]
	
	// if logevl is none,disable log
	if configLevel == -1 {
		return
	}
	
	if messageLevel >= configLevel {
		msg := fmt.Sprintf(format, args...)
		
		if strings.Contains(msg, "Failed to connect") {
			if level == "error" {
				level = "warn"
			}
		}
		
		if strings.Contains(msg, "Processing POST data") ||
		   strings.Contains(msg, "Client read error: read tcp") ||
		   strings.Contains(msg, "Client write error: write tcp") ||
		   strings.Contains(msg, "Remote read error") ||
		   strings.Contains(msg, "Remote write error") ||
		   strings.Contains(msg, "connection reset by peer") ||
		   strings.Contains(msg, "use of closed network connection") {
			return 
		}
		
		logger.Printf("[%s] %s", strings.ToUpper(level), msg)
	}
}

// Analy UUID
func parseUUID(uuidStr string) ([16]byte, error) {
	var uuid [16]byte
	uuidStr = strings.ReplaceAll(uuidStr, "-", "")
	if len(uuidStr) != 32 {
		return uuid, fmt.Errorf("invalid UUID length")
	}
	
	for i := 0; i < 16; i++ {
		b, err := strconv.ParseUint(uuidStr[i*2:i*2+2], 16, 8)
		if err != nil {
			return uuid, err
		}
		uuid[i] = byte(b)
	}
	return uuid, nil
}

// Analy VLS Header
func parseSimpleVlessHeader(data []byte) (string, uint16, []byte, error) {
	if len(data) < 18 {
		return "", 0, nil, fmt.Errorf("data too short")
	}
	
	// Verify UUID
	configUUID, _ := parseUUID(config.UUID)
	receivedUUID := data[1:17]
	
	for i := 0; i < 16; i++ {
		if receivedUUID[i] != configUUID[i] {
			return "", 0, nil, fmt.Errorf("UUID mismatch")
		}
	}
	
	pbLen := data[17]
	offset := 18 + int(pbLen)
	
	if len(data) < offset+4 {
		return "", 0, nil, fmt.Errorf("insufficient data for command/port/atype")
	}
	
	cmd := data[offset]
	if cmd != 1 {
		return "", 0, nil, fmt.Errorf("unsupported command: %d", cmd)
	}
	
	port := binary.BigEndian.Uint16(data[offset+1 : offset+3])
	atype := data[offset+3]
	offset += 4
	
	var hostname string
	switch atype {
	case 1: // IPv4
		if len(data) < offset+4 {
			return "", 0, nil, fmt.Errorf("insufficient data for IPv4")
		}
		hostname = fmt.Sprintf("%d.%d.%d.%d", data[offset], data[offset+1], data[offset+2], data[offset+3])
		offset += 4
	case 2: // domain
		if len(data) < offset+1 {
			return "", 0, nil, fmt.Errorf("insufficient data for domain length")
		}
		domainLen := int(data[offset])
		offset++
		if len(data) < offset+domainLen {
			return "", 0, nil, fmt.Errorf("insufficient data for domain")
		}
		hostname = string(data[offset : offset+domainLen])
		offset += domainLen
	case 3: // IPv6
		if len(data) < offset+16 {
			return "", 0, nil, fmt.Errorf("insufficient data for IPv6")
		}

		hostname = "::1"
		offset += 16
	default:
		return "", 0, nil, fmt.Errorf("unsupported address type: %d", atype)
	}
	
	remaining := data[offset:]
	return hostname, port, remaining, nil
}

// Handling VLS Connections - Using Hijack
func handleVlessConnection(w http.ResponseWriter, r *http.Request, uuid string) {

	hijacker, ok := w.(http.Hijacker)
	if !ok {
		http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
		return
	}
	
	conn, _, err := hijacker.Hijack()
	if err != nil {
		logf("error", "Failed to hijack connection: %v", err)
		return
	}
	defer conn.Close()
	
	logf("info", "Hijacked connection for session: %s", uuid)
	
	// send HTTP 
	httpResp := "HTTP/1.1 200 OK\r\n"
	httpResp += "Content-Type: application/octet-stream\r\n"
	httpResp += "Connection: close\r\n"
	httpResp += "\r\n"
	
	if _, err := conn.Write([]byte(httpResp)); err != nil {
		logf("error", "Failed to write HTTP response: %v", err)
		return
	}
	
	sessionsMu.Lock()
	session, exists := sessions[uuid]
	if !exists {
		session = &VlessSession{UUID: uuid}
		sessions[uuid] = session
		logf("debug", "Created session placeholder for: %s", uuid)
	}
	sessionsMu.Unlock()
	
	const maxWaitTime = 30 * time.Second 
	const checkInterval = 50 * time.Millisecond
	maxChecks := int(maxWaitTime / checkInterval)
	
	for i := 0; i < maxChecks; i++ {
		session.mu.Lock()
		if session.Remote != nil && session.ResponseHeader != nil {
			session.mu.Unlock()
			logf("debug", "Session initialized after %v for: %s", time.Duration(i)*checkInterval, uuid)
			break
		}
		session.mu.Unlock()
		
		if i > 0 && i%(int(5*time.Second/checkInterval)) == 0 {
			logf("debug", "Still waiting for session initialization: %s (%.1fs)", uuid, float64(i)*checkInterval.Seconds())
		}
		
		time.Sleep(checkInterval)
	}
	
	session.mu.Lock()
	if session.Remote == nil {
		session.mu.Unlock()
		logf("error", "Session not initialized within timeout: %s", uuid)
		return
	}
	
	if _, err := conn.Write(session.ResponseHeader); err != nil {
		session.mu.Unlock()
		logf("error", "Failed to write VLS response: %v", err)
		return
	}
	
	remote := session.Remote
	session.mu.Unlock()
	
	logf("info", "Starting data relay for session: %s", uuid)
	
	// Enable bidirectional data forwarding
	done := make(chan bool, 2)
	
	// From remote to client
	go func() {
		defer func() { done <- true }()
		buffer := make([]byte, config.ChunkSize)
		for {
			n, err := remote.Read(buffer)
			if err != nil {
				if err != io.EOF && !strings.Contains(err.Error(), "connection reset") {
					logf("warn", "Remote connection error for %s", uuid)
				}
				break
			}
			if n > 0 {
				if _, err := conn.Write(buffer[:n]); err != nil {
					if !strings.Contains(err.Error(), "connection reset") && !strings.Contains(err.Error(), "closed network connection") {
						logf("warn", "Client write error for %s", uuid)
					}
					break
				}
			}
		}
	}()
	
	// From the client to the remote
	go func() {
		defer func() { done <- true }()
		buffer := make([]byte, config.ChunkSize)
		for {
			n, err := conn.Read(buffer)
			if err != nil {
				if err != io.EOF && !strings.Contains(err.Error(), "connection reset") {
					logf("warn", "Client connection error for %s", uuid)
				}
				break
			}
			if n > 0 {
				if _, err := remote.Write(buffer[:n]); err != nil {
					if !strings.Contains(err.Error(), "connection reset") && !strings.Contains(err.Error(), "closed network connection") {
						logf("warn", "Remote write error for %s", uuid)
					}
					break
				}
			}
		}
	}()
	
	<-done
	logf("debug", "Session completed: %s", uuid)
	
	sessionsMu.Lock()
	delete(sessions, uuid)
	sessionsMu.Unlock()
}

// slove post data 
func handleVlessPost(w http.ResponseWriter, r *http.Request, uuid string, seq int) {
	body, err := io.ReadAll(r.Body)
	if err != nil {
		logf("error", "Failed to read POST body: %v", err)
		http.Error(w, "Bad Request", http.StatusBadRequest)
		return
	}
	
	if seq == 0 {
		logf("debug", "Initializing session: %s", uuid)
	}
	
	sessionsMu.Lock()
	session, exists := sessions[uuid]
	if !exists {
		session = &VlessSession{UUID: uuid}
		sessions[uuid] = session
	}
	sessionsMu.Unlock()
	
	if seq == 0 {
		hostname, port, remaining, err := parseSimpleVlessHeader(body)
		if err != nil {
			logf("error", "Failed to parse VLS header: %v", err)
			http.Error(w, "Bad Request", http.StatusBadRequest)
			return
		}
		
		logf("info", "VLS connection to %s:%d", hostname, port)
		
		if config.LogLevel == "debug" {
			go diagnoseConnection(hostname, port)
		}
		
		address := fmt.Sprintf("%s:%d", hostname, port)
		var remote net.Conn
		var connErr error
			
		for i := 0; i < 3; i++ {
			remote, connErr = net.DialTimeout("tcp", address, 15*time.Second)
			if connErr == nil {
				break
			}
			if i < 2 {
				logf("warn", "Connection attempt %d failed for %s: %v, retrying...", i+1, address, connErr)
				time.Sleep(1 * time.Second)
			}
		}
			
		if connErr != nil {
			logf("error", "Failed to connect to %s after 3 attempts: %v", address, connErr)
			http.Error(w, "Connection Failed", http.StatusBadGateway)
			return
		}
			
		logf("info", "Successfully connected to %s", address)
		
		if tcpConn, ok := remote.(*net.TCPConn); ok {
			tcpConn.SetNoDelay(true)
			tcpConn.SetKeepAlive(true)
		}
		
		session.mu.Lock()
		session.Remote = remote
		session.ResponseHeader = []byte{0x00, 0x00} 
		session.mu.Unlock()
		
		if len(remaining) > 0 {
			if _, err := remote.Write(remaining); err != nil {
				logf("error", "Failed to write initial data: %v", err)
			}
		}
		
		logf("info", "VLS session initialized: %s", uuid)
	} else {
		session.mu.Lock()
		if session.Remote != nil {
			_, err := session.Remote.Write(body)
			session.mu.Unlock()
			if err != nil {
				logf("error", "Failed to write data to remote: %v", err)
				http.Error(w, "Write Failed", http.StatusBadGateway)
				return
			}
		} else {
			session.mu.Unlock()
			logf("warn", "Received data for uninitialized session: %s", uuid)
		}
	}
	
	w.WriteHeader(http.StatusOK)
}

// get ISP info from Cloudflare
func getISPInfo() string {
	client := &http.Client{
		Timeout: 10 * time.Second,
	}
	
	resp, err := client.Get("https://speed.cloudflare.com/meta")
	if err != nil {
		logf("warn", "Failed to get ISP info from Cloudflare: %v", err)
		return "Unknown_ISP"
	}
	defer resp.Body.Close()
	
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		logf("warn", "Failed to read ISP response: %v", err)
		return "Unknown_ISP"
	}
	
	var meta CloudflareSpeedMeta
	if err := json.Unmarshal(body, &meta); err != nil {
		logf("warn", "Failed to parse ISP response: %v", err)
		return "Unknown_ISP"
	}
	
	country := strings.ReplaceAll(meta.Country, " ", "")
	asOrg := strings.ReplaceAll(meta.AsOrganization, " ", "_")
	asOrg = strings.ReplaceAll(asOrg, "(", "")
	asOrg = strings.ReplaceAll(asOrg, ")", "")
	
	isp := fmt.Sprintf("%s_%s", country, asOrg)
	logf("debug", "Got ISP info: %s (Country: %s, ASOrganization: %s)", isp, meta.Country, meta.AsOrganization)
	
	return isp
}

// http server
func handleRequest(w http.ResponseWriter, r *http.Request) {
	// root path
	if r.URL.Path == "/" {
		w.Header().Set("Content-Type", "text/plain")
		fmt.Fprint(w, "Hello, World\n")
		return
	}
	
	// sub route
	if r.URL.Path == "/"+config.SubPath {
		ip := config.Domain
		if ip == "" {
			ip = getServerIP()
		}
		
		if net.ParseIP(ip) == nil && !strings.Contains(ip, ".") {
			ip = "localhost"
			logf("warn", "Invalid IP format detected, using localhost")
		}
		
		if strings.Contains(ip, "<") || strings.Contains(ip, ">") {
			ip = "localhost"
			logf("warn", "HTML content detected in IP, using localhost")
		}
		
		isp := getISPInfo()
		nodeName := fmt.Sprintf("%s_%s", config.Name, isp)
		
		vlessURL := fmt.Sprintf("vless://%s@%s:443?encryption=none&security=tls&type=xhttp&host=%s&sni=%s&fp=chrome&path=%%2F%s&mode=packet-up#%s",
			config.UUID, ip, ip, ip, config.XPath, nodeName)
		
		logf("debug", "Generated subscription URL: %s", vlessURL)
		
		base64Content := base64.StdEncoding.EncodeToString([]byte(vlessURL))
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		fmt.Fprintf(w, "%s\n", base64Content)
		return
	}
	
	if !strings.Contains(r.URL.Path, config.XPath) {
		http.NotFound(w, r)
		return
	}
	
	// path prefix
	pathPrefix := fmt.Sprintf("/%s/", config.XPath)
	if !strings.HasPrefix(r.URL.Path, pathPrefix) {
		http.NotFound(w, r)
		return
	}
	
	remaining := strings.TrimPrefix(r.URL.Path, pathPrefix)
	parts := strings.Split(remaining, "/")
	
	if len(parts) < 1 || parts[0] == "" {
		http.NotFound(w, r)
		return
	}
	
	uuid := parts[0]
	
	if r.Method == "GET" && len(parts) == 1 {
		handleVlessConnection(w, r, uuid)
		return
	}
	
	if r.Method == "POST" && len(parts) == 2 {
		seq, err := strconv.Atoi(parts[1])
		if err != nil {
			http.NotFound(w, r)
			return
		}
		handleVlessPost(w, r, uuid, seq)
		return
	}
	
	http.NotFound(w, r)
}

// network diagnose
func diagnoseConnection(hostname string, port uint16) {
	address := fmt.Sprintf("%s:%d", hostname, port)
	
	ips, err := net.LookupIP(hostname)
	if err != nil {
		logf("warn", "DNS resolution failed for %s: %v", hostname, err)
		return
	}
	
	logf("debug", "DNS resolved %s to %v", hostname, ips)
	
	conn, err := net.DialTimeout("tcp", address, 5*time.Second)
	if err != nil {
		logf("warn", "Quick connection test failed for %s: %v", address, err)
		return
	}
	conn.Close()
	logf("debug", "Quick connection test successful for %s", address)
}

// get server IP
func getServerIP() string {
	services := []string{
		"https://ip.sb",
		"https://ipinfo.io/ip",
		"https://api.ipify.org",
		"https://ifconfig.me",

	}
	
	client := &http.Client{
		Timeout: 10 * time.Second,
	}
	
	for _, service := range services {
		resp, err := client.Get(service)
		if err != nil {
			continue
		}
		
		body, err := io.ReadAll(resp.Body)
		resp.Body.Close()
		
		if err != nil {
			continue
		}
		
		ip := strings.TrimSpace(string(body))
		
		if net.ParseIP(ip) != nil {
			logf("debug", "Got IP from %s: %s", service, ip)
			return ip
		}
	}
	
	// if all services fail, try to get local IP
	conn, err := net.Dial("udp", "8.8.8.8:80")
	if err == nil {
		defer conn.Close()
		localAddr := conn.LocalAddr().(*net.UDPAddr)
		logf("debug", "Using local IP: %s", localAddr.IP)
		return localAddr.IP.String()
	}
	
	logf("warn", "Failed to get server IP, using localhost")
	return "localhost"
}

// get nz download url
func getNezhaDownloadURL() (string, string) {
	if config.NezhaServer == "" || config.NezhaKey == "" {
		return "", ""
	}
	
	arch := runtime.GOARCH
	var version string
	
	if config.NezhaServer != "" && config.NezhaPort != "" && config.NezhaKey != "" {
		version = "agent"
	} else if config.NezhaServer != "" && config.NezhaKey != "" && strings.Contains(config.NezhaServer, ":") {
		version = "v1"
	} else {
		return "", ""
	}
	
	var url string
	if arch == "arm" || arch == "arm64" {
		url = fmt.Sprintf("https://arm64.ssss.nyc.mn/%s", version)
	} else {
		url = fmt.Sprintf("https://amd64.ssss.nyc.mn/%s", version)
	}
	
	return url, version
}

// download nz
func downloadNezhaAgent() (string, error) {
	url, version := getNezhaDownloadURL()
	if url == "" {
		return "", nil 
	}
	
	logf("info", "Downloading Nezha agent from %s", url)

	client := &http.Client{
		Timeout: 60 * time.Second,
	}
	
	resp, err := client.Get(url)
	if err != nil {
		return "", fmt.Errorf("failed to download: %v", err)
	}
	defer resp.Body.Close()
	
	file, err := os.Create("npm")
	if err != nil {
		return "", fmt.Errorf("failed to create file: %v", err)
	}
	defer file.Close()
	
	_, err = io.Copy(file, resp.Body)
	if err != nil {
		return "", fmt.Errorf("failed to save file: %v", err)
	}
	
	err = os.Chmod("npm", 0755)
	if err != nil {
		return "", fmt.Errorf("failed to set permissions: %v", err)
	}
	
	fmt.Println("nezha downloaded successfully")
	return version, nil
}

// create nz config
func createNezhaConfig(version string) error {
	if version == "agent" {
		return nil
	}
	
	if version == "v1" {
		server := config.NezhaServer
		var port string
		
		if strings.Contains(server, ":") {
			parts := strings.Split(server, ":")
			if len(parts) == 2 {
				server = parts[0]
				port = parts[1] 
			} else {
				return fmt.Errorf("invalid NEZHA_SERVER format: %s", config.NezhaServer)
			}
		} else {
			return fmt.Errorf("No port found in NEZHA_SERVER")
		}
		
		tlsPorts := []string{"443", "8443", "2096", "2087", "2083", "2053"}
		tls := "false"
		for _, tlsPort := range tlsPorts {
			if port == tlsPort {
				tls = "true"
				break
			}
		}
		
		serverAddr := fmt.Sprintf("%s:%s", server, port)
		
		configContent := fmt.Sprintf(`client_secret: %s
debug: false
disable_auto_update: true
disable_command_execute: false
disable_force_update: true
disable_nat: false
disable_send_query: false
gpu: false
insecure_tls: false
ip_report_period: 1800
report_delay: 4
server: %s
skip_connection_count: false
skip_procs_count: false
temperature: false
tls: %s
use_gitee_to_upgrade: false
use_ipv6_country_code: false
uuid: %s`,
			config.NezhaKey, serverAddr, tls, config.UUID)
		
		err := os.WriteFile("config.yaml", []byte(configContent), 0644)
		if err != nil {
			return fmt.Errorf("failed to create config file: %v", err)
		}
		
		logf("info", "Nezha v1 config file created for %s with TLS: %s", serverAddr, tls)
	}
	
	return nil
}

// run nz
func runNezhaAgent(version string) error {
	var cmd *exec.Cmd
	
	if version == "agent" {
		tlsPorts := []string{"443", "8443", "2096", "2087", "2083", "2053"}
		tlsFlag := ""
		for _, port := range tlsPorts {
			if config.NezhaPort == port {
				tlsFlag = "--tls"
				break
			}
		}
		
		server := fmt.Sprintf("%s:%s", config.NezhaServer, config.NezhaPort)
		args := []string{"-s", server, "-p", config.NezhaKey}
		if tlsFlag != "" {
			args = append(args, tlsFlag)
		}
		
		cmd = exec.Command("./npm", args...)
	} else if version == "v1" {
		cmd = exec.Command("./npm", "-c", "config.yaml")
	} else {
		return fmt.Errorf("unknown version: %s", version)
	}
	
	cmd.Stdout = nil
	cmd.Stderr = nil
	
	err := cmd.Start()
	if err != nil {
		return fmt.Errorf("failed to start nezha agent: %v", err)
	}
	
	fmt.Println("nezha is running")
	return nil
}

// download nz
func startNezhaAgent() {
	if config.NezhaServer == "" || config.NezhaKey == "" {
		logf("info", "Nezha agent doesn't enable")
		return
	}
	
	go func() {
		version, err := downloadNezhaAgent()
		if err != nil {
			logf("error", "Failed to download Nezha agent: %v", err)
			return
		}
		
		if version == "" {
			logf("info", "Nezha agent doesn't need to download")
			return
		}
		
		err = createNezhaConfig(version)
		if err != nil {
			logf("error", "Failed to create Nezha config: %v", err)
			return
		}
		
		time.Sleep(3 * time.Second)
		
		err = runNezhaAgent(version)
		if err != nil {
			logf("error", "Failed to run Nezha agent: %v", err)
			return
		}
	}()
}

// add auto access task
func addAccessTask() {
	if !config.AutoAccess {
		return
	}
	
	if config.Domain == "" {
		logf("warn", "AUTO_ACCESS enabled but DOMAIN is empty")
		return
	}
	
	fullURL := fmt.Sprintf("https://%s", config.Domain)
	command := fmt.Sprintf(`curl -X POST "https://oooo.serv00.net/add-url" -H "Content-Type: application/json" -d '{"url": "%s"}'`, fullURL)
	
	cmd := exec.Command("sh", "-c", command)
	err := cmd.Run()
	if err != nil {
		logf("error", "Error sending access task request: %v", err)
		return
	}
	
	fmt.Printf("Access task add successfully")
}

func main() {
	config = loadConfig()
	
	startNezhaAgent()

	addAccessTask()
	
	server := &http.Server{
		Addr:         ":" + config.Port,
		Handler:      http.HandlerFunc(handleRequest),
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
		IdleTimeout:  60 * time.Second,
	}
	
	fmt.Printf("Server is running on port %s\n", config.Port)
	if err := server.ListenAndServe(); err != nil {
		fmt.Printf("Server error: %v\n", err)
		logf("error", "Server error: %v", err)
	}
}