mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-05-26 23:19:47 +00:00
feat(names): Add dynamic name generation for peer
This commit is contained in:
@@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/openlibrecommunity/olcrtc/internal/client"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/names"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/server"
|
||||
)
|
||||
|
||||
@@ -16,6 +18,7 @@ func main() {
|
||||
socksPort int
|
||||
keyHex string
|
||||
debug bool
|
||||
dataDir string
|
||||
)
|
||||
|
||||
flag.StringVar(&mode, "mode", "", "Mode: srv or cnc")
|
||||
@@ -24,6 +27,7 @@ func main() {
|
||||
flag.IntVar(&socksPort, "socks-port", 1080, "SOCKS5 port (client only)")
|
||||
flag.StringVar(&keyHex, "key", "", "Shared encryption key (hex)")
|
||||
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
|
||||
flag.StringVar(&dataDir, "data", "data", "Path to data directory")
|
||||
flag.Parse()
|
||||
|
||||
if !debug {
|
||||
@@ -38,6 +42,13 @@ func main() {
|
||||
log.Fatal("Room ID required")
|
||||
}
|
||||
|
||||
namesPath := filepath.Join(dataDir, "names")
|
||||
surnamesPath := filepath.Join(dataDir, "surnames")
|
||||
|
||||
if err := names.LoadNameFiles(namesPath, surnamesPath); err != nil {
|
||||
log.Fatalf("Failed to load name files: %v", err)
|
||||
}
|
||||
|
||||
roomURL := "https://telemost.yandex.ru/j/" + roomID
|
||||
|
||||
switch mode {
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/pion/webrtc/v4"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/crypto"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/mux"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/names"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/telemost"
|
||||
)
|
||||
|
||||
@@ -58,7 +59,7 @@ func Run(roomURL, keyHex string, socksPort int) error {
|
||||
return c.peer.Send(encrypted)
|
||||
})
|
||||
|
||||
peer, err := telemost.NewPeer(roomURL, "OlcRTC-Client", c.onData)
|
||||
peer, err := telemost.NewPeer(roomURL, names.Generate(), c.onData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
63
internal/names/names.go
Normal file
63
internal/names/names.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package names
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
firstNames []string
|
||||
lastNames []string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func loadNames(path string) ([]string, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var names []string
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line != "" {
|
||||
names = append(names, line)
|
||||
}
|
||||
}
|
||||
|
||||
return names, scanner.Err()
|
||||
}
|
||||
|
||||
func LoadNameFiles(firstPath, lastPath string) error {
|
||||
var err error
|
||||
firstNames, err = loadNames(firstPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lastNames, err = loadNames(lastPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Generate() string {
|
||||
if len(firstNames) == 0 || len(lastNames) == 0 {
|
||||
return "Unknown User"
|
||||
}
|
||||
|
||||
first := firstNames[rand.Intn(len(firstNames))]
|
||||
last := lastNames[rand.Intn(len(lastNames))]
|
||||
|
||||
return first + " " + last
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/pion/webrtc/v4"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/crypto"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/mux"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/names"
|
||||
"github.com/openlibrecommunity/olcrtc/internal/telemost"
|
||||
)
|
||||
|
||||
@@ -66,7 +67,7 @@ func Run(roomURL, keyHex string) error {
|
||||
return s.peer.Send(encrypted)
|
||||
})
|
||||
|
||||
peer, err := telemost.NewPeer(roomURL, "OlcRTC-Server", s.onData)
|
||||
peer, err := telemost.NewPeer(roomURL, names.Generate(), s.onData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user