123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- package streamproxy
- import (
- "encoding/json"
- "errors"
- "net"
- "os"
- "path/filepath"
- "sync"
- "sync/atomic"
- "time"
- "github.com/google/uuid"
- "imuslab.com/zoraxy/mod/info/logger"
- "imuslab.com/zoraxy/mod/utils"
- )
- type ProxyRelayOptions struct {
- Name string
- ListeningAddr string
- ProxyAddr string
- Timeout int
- UseTCP bool
- UseUDP bool
- }
- type ProxyRelayConfig struct {
- UUID string
- Name string
- Running bool
- AutoStart bool
- ListeningAddress string
- ProxyTargetAddr string
- UseTCP bool
- UseUDP bool
- Timeout int
- tcpStopChan chan bool
- udpStopChan chan bool
- aTobAccumulatedByteTransfer atomic.Int64
- bToaAccumulatedByteTransfer atomic.Int64
- udpClientMap sync.Map
- parent *Manager `json:"-"`
- }
- type Options struct {
- DefaultTimeout int
- AccessControlHandler func(net.Conn) bool
- ConfigStore string
- Logger *logger.Logger
- }
- type Manager struct {
-
- Options *Options
- Configs []*ProxyRelayConfig
-
- Connections int
- }
- func NewStreamProxy(options *Options) (*Manager, error) {
- if !utils.FileExists(options.ConfigStore) {
- err := os.MkdirAll(options.ConfigStore, 0775)
- if err != nil {
- return nil, err
- }
- }
-
- previousRules := []*ProxyRelayConfig{}
- streamProxyConfigFiles, err := filepath.Glob(options.ConfigStore + "/*.config")
- if err != nil {
- return nil, err
- }
- for _, configFile := range streamProxyConfigFiles {
-
- configBytes, err := os.ReadFile(configFile)
- if err != nil {
- options.Logger.PrintAndLog("stream-prox", "Read stream proxy config failed", err)
- continue
- }
- thisRelayConfig := &ProxyRelayConfig{}
- err = json.Unmarshal(configBytes, thisRelayConfig)
- if err != nil {
- options.Logger.PrintAndLog("stream-prox", "Unmarshal stream proxy config failed", err)
- continue
- }
-
- previousRules = append(previousRules, thisRelayConfig)
- }
-
- if options.AccessControlHandler == nil {
- options.AccessControlHandler = func(conn net.Conn) bool {
-
- return true
- }
- }
-
- thisManager := Manager{
- Options: options,
- Connections: 0,
- }
-
- for _, rule := range previousRules {
- rule.parent = &thisManager
- if rule.Running {
-
- thisManager.logf("Resuming stream proxy rule "+rule.Name, nil)
- rule.Start()
- }
- }
- thisManager.Configs = previousRules
- return &thisManager, nil
- }
- func (m *Manager) logf(message string, originalError error) {
- if m.Options.Logger == nil {
-
- if originalError != nil {
- message += ": " + originalError.Error()
- }
- println(message)
- return
- }
- m.Options.Logger.PrintAndLog("stream-prox", message, originalError)
- }
- func (m *Manager) NewConfig(config *ProxyRelayOptions) string {
-
- aAcc := atomic.Int64{}
- bAcc := atomic.Int64{}
- aAcc.Store(0)
- bAcc.Store(0)
-
- configUUID := uuid.New().String()
- thisConfig := ProxyRelayConfig{
- UUID: configUUID,
- Name: config.Name,
- ListeningAddress: config.ListeningAddr,
- ProxyTargetAddr: config.ProxyAddr,
- UseTCP: config.UseTCP,
- UseUDP: config.UseUDP,
- Timeout: config.Timeout,
- tcpStopChan: nil,
- udpStopChan: nil,
- aTobAccumulatedByteTransfer: aAcc,
- bToaAccumulatedByteTransfer: bAcc,
- udpClientMap: sync.Map{},
- parent: m,
- }
- m.Configs = append(m.Configs, &thisConfig)
- m.SaveConfigToDatabase()
- return configUUID
- }
- func (m *Manager) GetConfigByUUID(configUUID string) (*ProxyRelayConfig, error) {
-
- for _, config := range m.Configs {
- if config.UUID == configUUID {
- return config, nil
- }
- }
- return nil, errors.New("config not found")
- }
- func (m *Manager) EditConfig(configUUID string, newName string, newListeningAddr string, newProxyAddr string, useTCP bool, useUDP bool, newTimeout int) error {
-
- foundConfig, err := m.GetConfigByUUID(configUUID)
- if err != nil {
- return err
- }
-
- if newName != "" {
- foundConfig.Name = newName
- }
- if newListeningAddr != "" {
- foundConfig.ListeningAddress = newListeningAddr
- }
- if newProxyAddr != "" {
- foundConfig.ProxyTargetAddr = newProxyAddr
- }
- foundConfig.UseTCP = useTCP
- foundConfig.UseUDP = useUDP
- if newTimeout != -1 {
- if newTimeout < 0 {
- return errors.New("invalid timeout value given")
- }
- foundConfig.Timeout = newTimeout
- }
- m.SaveConfigToDatabase()
-
- if foundConfig.IsRunning() {
- foundConfig.Restart()
- }
- return nil
- }
- func (m *Manager) RemoveConfig(configUUID string) error {
-
- err := os.Remove(filepath.Join(m.Options.ConfigStore, configUUID+".config"))
- if err != nil {
- return err
- }
-
- for i, config := range m.Configs {
- if config.UUID == configUUID {
- m.Configs = append(m.Configs[:i], m.Configs[i+1:]...)
- m.SaveConfigToDatabase()
- return nil
- }
- }
- return errors.New("config not found")
- }
- func (m *Manager) SaveConfigToDatabase() {
- for _, config := range m.Configs {
- configBytes, err := json.Marshal(config)
- if err != nil {
- m.logf("Failed to marshal stream proxy config", err)
- continue
- }
- err = os.WriteFile(m.Options.ConfigStore+"/"+config.UUID+".config", configBytes, 0775)
- if err != nil {
- m.logf("Failed to save stream proxy config", err)
- }
- }
- }
- func (c *ProxyRelayConfig) Start() error {
- if c.IsRunning() {
- c.Running = true
- return errors.New("proxy already running")
- }
-
- tcpStopChan := make(chan bool)
- udpStopChan := make(chan bool)
-
- if c.UseUDP {
- c.udpStopChan = udpStopChan
- go func() {
- err := c.ForwardUDP(c.ListeningAddress, c.ProxyTargetAddr, udpStopChan)
- if err != nil {
- if !c.UseTCP {
- c.Running = false
- c.udpStopChan = nil
- c.parent.SaveConfigToDatabase()
- }
- c.parent.logf("[proto:udp] Error starting stream proxy "+c.Name+"("+c.UUID+")", err)
- }
- }()
- }
- if c.UseTCP {
- c.tcpStopChan = tcpStopChan
- go func() {
-
- err := c.Port2host(c.ListeningAddress, c.ProxyTargetAddr, tcpStopChan)
- if err != nil {
- c.Running = false
- c.tcpStopChan = nil
- c.parent.SaveConfigToDatabase()
- c.parent.logf("[proto:tcp] Error starting stream proxy "+c.Name+"("+c.UUID+")", err)
- }
- }()
- }
-
- c.Running = true
- c.parent.SaveConfigToDatabase()
- return nil
- }
- func (c *ProxyRelayConfig) IsRunning() bool {
- return c.tcpStopChan != nil || c.udpStopChan != nil
- }
- func (c *ProxyRelayConfig) Restart() {
- if c.IsRunning() {
- c.Stop()
- }
- time.Sleep(3000 * time.Millisecond)
- c.Start()
- }
- func (c *ProxyRelayConfig) Stop() {
- c.parent.logf("Stopping Stream Proxy "+c.Name, nil)
- if c.udpStopChan != nil {
- c.parent.logf("Stopping UDP for "+c.Name, nil)
- c.udpStopChan <- true
- c.udpStopChan = nil
- }
- if c.tcpStopChan != nil {
- c.parent.logf("Stopping TCP for "+c.Name, nil)
- c.tcpStopChan <- true
- c.tcpStopChan = nil
- }
- c.parent.logf("Stopped Stream Proxy "+c.Name, nil)
- c.Running = false
-
- c.parent.SaveConfigToDatabase()
- }
|