feat: авторизация по client-id, freeturn:// URI и подписки

- internal/clientsdb: БД клиентов с hot-reload + length-prefixed ID wire
- internal/uri: parse/encode share-ссылок freeturn://
- internal/sub: загрузка и парсинг подписок (sub.md)
- client: флаги -auth, -client-id, -sub; позиционный URI; ID через crypto/rand
- server: подкоманда clients (add/remove/list), чтение ID под -clients-file
- запись/чтение ID учитывают DTLS record-orientation (буфер на запись целиком)
- docs: uri.md, sub.md; правки flags/deploy/quickstart
- снят UTF-8 BOM по всем исходникам
This commit is contained in:
samosvalishe committed 2026-05-24 01:14:38 +03:00
1 parent 41d877dc64
commit e061345089
78 files changed
+1151 -82

No files matched your search

+151
View File
@@ -0,0 +1,151 @@
package sub
import (
"bufio"
"context"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/samosvalishe/free-turn-proxy/internal/uri"
)
// Sub представляет распарсенную подписку
type Sub struct {
Name string
Update string
Refresh string
Color string
Icon string
Used string
Available string
Nodes []Node
}
// Node представляет один сервер из подписки
type Node struct {
URI *uri.Config
Name string
Color string
Icon string
Used string
Available string
IP string
Comment string
}
// Fetch скачивает и парсит подписку по URL
func Fetch(ctx context.Context, url string) (*Sub, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return Parse(resp.Body)
}
// Parse читает Reader и возвращает структуру Sub
func Parse(r io.Reader) (*Sub, error) {
s := &Sub{}
scanner := bufio.NewScanner(r)
var lastNode *Node
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
// Глобальные поля
if strings.HasPrefix(line, "#") && !strings.HasPrefix(line, "##") {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0][1:])
val := strings.TrimSpace(parts[1])
switch key {
case "name":
s.Name = val
case "update":
s.Update = val
case "refresh":
s.Refresh = val
case "color":
s.Color = val
case "icon":
s.Icon = val
case "used":
s.Used = val
case "available":
s.Available = val
}
}
continue
}
// Локальные поля
if strings.HasPrefix(line, "##") {
if lastNode == nil {
continue // Нет привязанного URI
}
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0][2:])
val := strings.TrimSpace(parts[1])
switch key {
case "name":
lastNode.Name = val
case "color":
lastNode.Color = val
case "icon":
lastNode.Icon = val
case "used":
lastNode.Used = val
case "available":
lastNode.Available = val
case "ip":
lastNode.IP = val
case "comment":
lastNode.Comment = val
}
}
continue
}
// URI (freeturn://)
if strings.HasPrefix(line, "freeturn://") {
cfg, err := uri.Parse(line)
if err == nil {
node := Node{URI: cfg}
s.Nodes = append(s.Nodes, node)
lastNode = &s.Nodes[len(s.Nodes)-1]
} else {
log.Printf("warning: skipped invalid freeturn URI in subscription: %v", err)
}
continue
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return s, nil
}
+57
View File
@@ -0,0 +1,57 @@
package sub
import (
"strings"
"testing"
)
func TestParse(t *testing.T) {
data := `#name: Test Sub
#refresh: 1h
#color: #123456
freeturn://vk?tcp<mode=tcpfwd>@1.1.1.1:56000#key$comment1
##name: Server 1
##ip: 1.1.1.1
freeturn://vk@2.2.2.2
##name: Server 2
`
s, err := Parse(strings.NewReader(data))
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if s.Name != "Test Sub" {
t.Errorf("Sub.Name = %v, want Test Sub", s.Name)
}
if s.Refresh != "1h" {
t.Errorf("Sub.Refresh = %v, want 1h", s.Refresh)
}
if s.Color != "#123456" {
t.Errorf("Sub.Color = %v, want #123456", s.Color)
}
if len(s.Nodes) != 2 {
t.Fatalf("len(Nodes) = %d, want 2", len(s.Nodes))
}
n1 := s.Nodes[0]
if n1.Name != "Server 1" {
t.Errorf("Node[0].Name = %v, want Server 1", n1.Name)
}
if n1.IP != "1.1.1.1" {
t.Errorf("Node[0].IP = %v, want 1.1.1.1", n1.IP)
}
if n1.URI.Provider != "vk" || n1.URI.Transport != "tcp" {
t.Errorf("Node[0].URI = %+v", n1.URI)
}
n2 := s.Nodes[1]
if n2.Name != "Server 2" {
t.Errorf("Node[1].Name = %v, want Server 2", n2.Name)
}
if n2.URI.Peer != "2.2.2.2" {
t.Errorf("Node[1].URI.Peer = %v, want 2.2.2.2", n2.URI.Peer)
}
}