Skip to content

Commit

Permalink
update to go-observer v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 12, 2024
1 parent d075d2d commit 407fd92
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ jobs:
GOAMD64: ${{ matrix.goamd64 }}
GOARM: ${{ matrix.goarm }}
GOMIPS: ${{ matrix.gomips }}
CGO_ENABLED: 0
TAGS: with_clash_api,with_quic
CGO_ENABLED: 1
TAGS: with_gvisor,with_quic,with_wireguard,with_ech,with_utls,with_clash_api,with_grpc
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
Expand Down
8 changes: 5 additions & 3 deletions common/urltest/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"sync"
"time"

"github.com/imkira/go-observer"
"github.com/imkira/go-observer/v2"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
Expand All @@ -22,7 +23,7 @@ type History struct {
type HistoryStorage struct {
access sync.RWMutex
delayHistory map[string]*History
updateHook observer.Property
updateHook observer.Property[int]
}

func NewHistoryStorage() *HistoryStorage {
Expand All @@ -31,7 +32,8 @@ func NewHistoryStorage() *HistoryStorage {
}
}

func (s *HistoryStorage) SetHook(hook observer.Property) {
func (s *HistoryStorage) SetHook(hook observer.Property[int]) {
log.Warn("Hiddify! Setting Hook")
s.updateHook = hook
}

Expand Down
5 changes: 5 additions & 0 deletions experimental/libbox/command_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/urltest"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/outbound"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/rw"
Expand Down Expand Up @@ -69,6 +70,7 @@ func (s *CommandServer) handleGroupConn(conn net.Conn, onlyGroupItems bool) erro
defer ticker.Stop()
ctx := connKeepAlive(conn)
urlTestUpdateStream := s.urlTestUpdate.Observe()
log.Warn("Hiddify! urlTestUpdateStream", urlTestUpdateStream)
for {
service := s.service
if service != nil {
Expand All @@ -87,10 +89,13 @@ func (s *CommandServer) handleGroupConn(conn net.Conn, onlyGroupItems bool) erro
return ctx.Err()
case <-ticker.C:
}
log.Warn("Hiddify! Timeout! Waiting for a change")
select {
case <-ctx.Done():
return ctx.Err()
case <-urlTestUpdateStream.Changes():
val := urlTestUpdateStream.Next()
log.Warn("Hiddify! Receive a change", val)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion experimental/libbox/command_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"net"

"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/outbound"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/rw"
Expand Down Expand Up @@ -55,6 +56,7 @@ func (s *CommandServer) handleSelectOutbound(conn net.Conn) error {
if !selector.SelectOutbound(outboundTag) {
return writeError(conn, E.New("outbound not found in selector: ", outboundTag))
}
s.urlTestUpdate.Update(1)
log.Warn("Hiddify! Command Select", outboundTag)
s.urlTestUpdate.Update(2)
return writeError(conn, nil)
}
6 changes: 4 additions & 2 deletions experimental/libbox/command_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"sync"

"github.com/imkira/go-observer"
"github.com/imkira/go-observer/v2"
"github.com/sagernet/sing-box/common/urltest"
"github.com/sagernet/sing-box/experimental/clashapi"
"github.com/sagernet/sing-box/log"
Expand All @@ -31,7 +31,7 @@ type CommandServer struct {
service *BoxService

// These channels only work with a single client. if multi-client support is needed, replace with Subscriber/Observer
urlTestUpdate observer.Property
urlTestUpdate observer.Property[int]
modeUpdate chan struct{}
logReset chan struct{}
}
Expand All @@ -57,6 +57,7 @@ func NewCommandServer(handler CommandServerHandler, maxLines int32) *CommandServ

func (s *CommandServer) SetService(newService *BoxService) {
if newService != nil {
log.Warn("Hiddify! Setting service to ", newService)
service.PtrFromContext[urltest.HistoryStorage](newService.ctx).SetHook(s.urlTestUpdate)
newService.instance.Router().ClashServer().(*clashapi.Server).SetModeUpdateHook(s.modeUpdate)
s.savedLines.Init()
Expand All @@ -75,6 +76,7 @@ func (s *CommandServer) notifyURLTestUpdate() {
// default:
// }
s.urlTestUpdate.Update(1)
log.Warn("Hiddify! URL Test Update")
}

func (s *CommandServer) Start() error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.3
github.com/gofrs/uuid/v5 v5.0.0
github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b
github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2
github.com/libdns/alidns v1.0.3
github.com/libdns/cloudflare v0.1.0
Expand Down Expand Up @@ -71,7 +72,6 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/imkira/go-observer v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1 h1:neOb+wzHbWLNZ2sHFEV4+GTuqORO7/MndQLFW8FjUY8=
github.com/hiddify/wireguard-go v0.0.0-20240125143346-481d18d77fe1/go.mod h1:K4J7/npM+VAMUeUmTa2JaA02JmyheP0GpRBOUvn3ecc=
github.com/imkira/go-observer v1.0.3 h1:l45TYAEeAB4L2xF6PR2gRLn2NE5tYhudh33MLmC7B80=
github.com/imkira/go-observer v1.0.3/go.mod h1:zLzElv2cGTHufQG17IEILJMPDg32TD85fFgKyFv00wU=
github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b h1:1+115FqGoS8p6Iry9AYmrcWDvSveH0F7P2nX1LU00qg=
github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b/go.mod h1:XCscqBi1KKh7GcVDDAdkT/Cf6WDjnDAA1XM3nwmA0Ag=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 h1:9K06NfxkBh25x56yVhWWlKFE8YpicaSfHwoV8SFbueA=
Expand Down

0 comments on commit 407fd92

Please sign in to comment.