-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: set V2Ray API in configuration (#138)
Stats and servers, a dance so grand, With every change, more in command. Modular ways, the future bright, For network paths, a guiding light. 🌟
- Loading branch information
1 parent
846ec04
commit 3dcc6e5
Showing
6 changed files
with
150 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package v2rayapilite | ||
|
||
import ( | ||
"github.com/sagernet/sing-box/adapter" | ||
"github.com/sagernet/sing-box/experimental" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/sagernet/sing-box/option" | ||
"github.com/sagernet/sing/common" | ||
) | ||
|
||
func init() { | ||
experimental.RegisterV2RayServerConstructor(NewServer) | ||
} | ||
|
||
var ( | ||
_ adapter.V2RayServer = (*V2rayServer)(nil) | ||
_ StatsGetter = (*V2rayServer)(nil) | ||
) | ||
|
||
type StatsGetter interface { | ||
QueryStats(name string) int64 | ||
} | ||
|
||
type V2rayServer struct { | ||
statsService *StatsService | ||
} | ||
|
||
func (v *V2rayServer) QueryStats(name string) int64 { | ||
return v.statsService.QueryStats(name) | ||
} | ||
|
||
func NewServer(_ log.Logger, options option.V2RayAPIOptions) (adapter.V2RayServer, error) { | ||
return &V2rayServer{ | ||
statsService: NewStatsService(common.PtrValueOrDefault(options.Stats)), | ||
}, nil | ||
} | ||
|
||
func (v *V2rayServer) Start() error { | ||
return nil | ||
} | ||
|
||
func (v *V2rayServer) Close() error { | ||
return nil | ||
} | ||
|
||
func (v *V2rayServer) StatsService() adapter.V2RayStatsService { | ||
return v.statsService | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package v2rayapilite | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/sagernet/sing-box/adapter" | ||
"github.com/sagernet/sing-box/option" | ||
"github.com/sagernet/sing/common/atomic" | ||
"github.com/sagernet/sing/common/bufio" | ||
N "github.com/sagernet/sing/common/network" | ||
) | ||
|
||
var ( | ||
_ adapter.V2RayStatsService = (*StatsService)(nil) | ||
_ StatsGetter = (*StatsService)(nil) | ||
) | ||
|
||
type StatsService struct { | ||
createdAt time.Time | ||
outbounds map[string]bool | ||
access sync.Mutex | ||
counters map[string]*atomic.Int64 | ||
} | ||
|
||
func NewStatsService(options option.V2RayStatsServiceOptions) *StatsService { | ||
if !options.Enabled { | ||
return nil | ||
} | ||
outbounds := make(map[string]bool) | ||
for _, outbound := range options.Outbounds { | ||
outbounds[outbound] = true | ||
} | ||
return &StatsService{ | ||
createdAt: time.Now(), | ||
outbounds: outbounds, | ||
counters: make(map[string]*atomic.Int64), | ||
} | ||
} | ||
|
||
func (s *StatsService) QueryStats(name string) int64 { | ||
s.access.Lock() | ||
counter, loaded := s.counters[name] | ||
s.access.Unlock() | ||
if !loaded { | ||
return 0 | ||
} | ||
|
||
return counter.Swap(0) | ||
} | ||
|
||
func (s *StatsService) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn { | ||
var readCounter []*atomic.Int64 | ||
var writeCounter []*atomic.Int64 | ||
countOutbound := outbound != "" && s.outbounds[outbound] | ||
if !countOutbound { | ||
return conn | ||
} | ||
s.access.Lock() | ||
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink")) | ||
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink")) | ||
s.access.Unlock() | ||
return bufio.NewInt64CounterConn(conn, readCounter, writeCounter) | ||
} | ||
|
||
func (s *StatsService) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn { | ||
var readCounter []*atomic.Int64 | ||
var writeCounter []*atomic.Int64 | ||
countOutbound := outbound != "" && s.outbounds[outbound] | ||
if !countOutbound { | ||
return conn | ||
} | ||
s.access.Lock() | ||
readCounter = append(readCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>uplink")) | ||
writeCounter = append(writeCounter, s.loadOrCreateCounter("outbound>>>"+outbound+">>>traffic>>>downlink")) | ||
s.access.Unlock() | ||
return bufio.NewInt64CounterPacketConn(conn, readCounter, writeCounter) | ||
} | ||
|
||
//nolint:staticcheck | ||
func (s *StatsService) loadOrCreateCounter(name string) *atomic.Int64 { | ||
counter, loaded := s.counters[name] | ||
if loaded { | ||
return counter | ||
} | ||
counter = &atomic.Int64{} | ||
s.counters[name] = counter | ||
return counter | ||
} |
This file was deleted.
Oops, something went wrong.