Skip to content

Commit

Permalink
update for better handling the background service
Browse files Browse the repository at this point in the history
  • Loading branch information
x committed Dec 28, 2024
1 parent ff4c3d8 commit fcd117d
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 226 deletions.
2 changes: 1 addition & 1 deletion extension/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ func (s *extensionService) OnMainServiceClose() error {
}

func init() {
service_manager.Register(&extensionService{})
// service_manager.Register(&extensionService{})
}
156 changes: 0 additions & 156 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,159 +1,3 @@
// module github.com/hiddify/hiddify-core

// go 1.22.0

// toolchain go1.22.3

// require (
// github.com/bepass-org/warp-plus v1.2.4
// github.com/google/uuid v1.6.0
// github.com/hiddify/hiddify-app-demo-extension v0.0.0-20241001070003-26039f960ad6
// github.com/hiddify/ray2sing v0.0.0-20240804185422-f340989b59a0
// github.com/improbable-eng/grpc-web v0.15.0
// github.com/jellydator/validation v1.1.0
// github.com/kardianos/service v1.2.2
// github.com/sagernet/gomobile v0.1.4
// github.com/sagernet/sing v0.4.3
// github.com/sagernet/sing-box v1.8.9
// github.com/sagernet/sing-dns v0.2.3
// github.com/spf13/cobra v1.8.0
// github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
// github.com/tendermint/tm-db v0.6.7
// github.com/xmdhs/clash2singbox v0.0.2
// golang.org/x/net v0.28.0
// golang.org/x/sys v0.25.0
// google.golang.org/grpc v1.67.0
// google.golang.org/protobuf v1.35.1
// gopkg.in/yaml.v3 v3.0.1
// )

// require (
// berty.tech/go-libtor v1.0.385 // indirect
// github.com/DataDog/zstd v1.4.1 // indirect
// github.com/ajg/form v1.5.1 // indirect
// github.com/andybalholm/brotli v1.1.0 // indirect
// github.com/caddyserver/certmagic v0.20.0 // indirect
// github.com/cenkalti/backoff/v4 v4.1.1 // indirect
// github.com/cespare/xxhash v1.1.0 // indirect
// github.com/cloudflare/circl v1.4.0 // indirect
// github.com/cosmos/gorocksdb v1.2.0 // indirect
// github.com/cretz/bine v0.2.0 // indirect
// github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
// github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
// github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de // indirect
// github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
// github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
// github.com/dustin/go-humanize v1.0.0 // indirect
// github.com/fatih/color v1.16.0 // indirect
// github.com/francoispqt/gojay v1.2.13 // indirect
// github.com/fsnotify/fsnotify v1.7.0 // indirect
// github.com/gaukas/godicttls v0.0.4 // indirect
// github.com/ghodss/yaml v1.0.1-0.20220118164431-d8423dcdf344 // indirect
// github.com/go-chi/chi/v5 v5.0.12 // indirect
// github.com/go-chi/cors v1.2.1 // indirect
// github.com/go-chi/render v1.0.3 // indirect
// github.com/go-ole/go-ole v1.3.0 // indirect
// github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
// github.com/gobwas/httphead v0.1.0 // indirect
// github.com/gobwas/pool v0.2.1 // indirect
// github.com/gofrs/uuid/v5 v5.2.0 // indirect
// github.com/golang/protobuf v1.5.4 // indirect
// github.com/golang/snappy v0.0.1 // indirect
// github.com/google/btree v1.1.2 // indirect
// github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
// github.com/gorilla/websocket v1.5.3 // indirect
// github.com/hashicorp/yamux v0.1.1 // indirect
// github.com/imkira/go-observer/v2 v2.0.0-20230629064422-8e0b61f11f1b // indirect
// github.com/inconshreveable/mousetrap v1.1.0 // indirect
// github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 // indirect
// github.com/jmhodges/levigo v1.0.0 // indirect
// github.com/josharian/native v1.1.0 // indirect
// github.com/klauspost/compress v1.17.8 // indirect
// github.com/klauspost/cpuid/v2 v2.2.7 // indirect
// github.com/libdns/alidns v1.0.3 // indirect
// github.com/libdns/cloudflare v0.1.1 // indirect
// github.com/libdns/libdns v0.2.2 // indirect
// github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
// github.com/mattn/go-colorable v0.1.13 // indirect
// github.com/mattn/go-isatty v0.0.20 // indirect
// github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d // indirect
// github.com/mholt/acmez v1.2.0 // indirect
// github.com/miekg/dns v1.1.62 // indirect
// github.com/modern-go/reflect2 v1.0.2 // indirect
// github.com/onsi/ginkgo/v2 v2.19.0 // indirect
// github.com/ooni/go-libtor v1.1.8 // indirect
// github.com/oschwald/maxminddb-golang v1.12.0 // indirect
// github.com/pelletier/go-toml v1.9.5 // indirect
// github.com/pierrec/lz4/v4 v4.1.14 // indirect
// github.com/pion/dtls/v2 v2.2.7 // indirect
// github.com/pion/logging v0.2.2 // indirect
// github.com/pion/randutil v0.1.0 // indirect
// github.com/pion/stun/v2 v2.0.0 // indirect
// github.com/pion/transport/v2 v2.2.3 // indirect
// github.com/pion/transport/v3 v3.0.1 // indirect
// github.com/pion/turn/v3 v3.0.1 // indirect
// github.com/pires/go-proxyproto v0.7.0 // indirect
// github.com/pkg/errors v0.9.1 // indirect
// github.com/quic-go/qpack v0.4.0 // indirect
// github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
// github.com/quic-go/quic-go v0.46.0 // indirect
// github.com/refraction-networking/utls v1.6.7 // indirect
// github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
// github.com/rs/cors v1.7.0 // indirect
// github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a // indirect
// github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 // indirect
// github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f // indirect
// github.com/sagernet/netlink v0.0.0-20240523065131-45e60152f9ba // indirect
// github.com/sagernet/quic-go v0.47.0-beta.2 // indirect
// github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 // indirect
// github.com/sagernet/sing-mux v0.2.0 // indirect
// github.com/sagernet/sing-quic v0.2.2 // indirect
// github.com/sagernet/sing-shadowsocks v0.2.7 // indirect
// github.com/sagernet/sing-shadowsocks2 v0.2.0 // indirect
// github.com/sagernet/sing-shadowtls v0.1.4 // indirect
// github.com/sagernet/sing-tun v0.3.3 // indirect
// github.com/sagernet/sing-vmess v0.1.12 // indirect
// github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 // indirect
// github.com/sagernet/utls v1.5.4 // indirect
// github.com/sagernet/wireguard-go v0.0.0-20231215174105-89dec3b2f3e8 // indirect
// github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 // indirect
// github.com/seiflotfy/cuckoofilter v0.0.0-20240715131351-a2f2c23f1771 // indirect
// github.com/spf13/pflag v1.0.5 // indirect
// github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect
// github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e // indirect
// github.com/vishvananda/netns v0.0.4 // indirect
// github.com/xtls/reality v0.0.0-20240712055506-48f0b2d5ed6d // indirect
// github.com/xtls/xray-core v1.8.21 // indirect
// github.com/zeebo/blake3 v0.2.3 // indirect
// go.etcd.io/bbolt v1.3.6 // indirect
// go.uber.org/mock v0.4.0 // indirect
// go.uber.org/multierr v1.11.0 // indirect
// go.uber.org/zap v1.27.0 // indirect
// go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
// golang.org/x/crypto v0.26.0 // indirect
// golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect
// golang.org/x/mod v0.18.0 // indirect
// golang.org/x/sync v0.8.0 // indirect
// golang.org/x/text v0.18.0 // indirect
// golang.org/x/time v0.5.0 // indirect
// golang.org/x/tools v0.22.0 // indirect
// google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
// gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
// gopkg.in/yaml.v2 v2.4.0 // indirect
// lukechampine.com/blake3 v1.3.0 // indirect
// nhooyr.io/websocket v1.8.6 // indirect
// )

// replace github.com/sagernet/sing-box => github.com/hiddify/hiddify-sing-box v1.8.9-0.20241013222038-47abad60f848

// replace github.com/xtls/xray-core => github.com/hiddify/xray-core v0.0.0-20240902024714-0fcb0895bb4b

// replace github.com/sagernet/wireguard-go => github.com/hiddify/wireguard-go v0.0.0-20240727191222-383c1da14ff1

// replace github.com/bepass-org/warp-plus => github.com/hiddify/warp-plus v0.0.0-20240717223357-4f3122e0d11d

// replace github.com/hiddify/ray2sing => github.com/hiddify/ray2sing v0.0.0-20240928221833-190b549d5222

module github.com/hiddify/hiddify-core

go 1.22.0
Expand Down
8 changes: 4 additions & 4 deletions platform/mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/sagernet/sing-box/experimental/libbox"
)

func Setup(baseDir string, workingDir string, tempDir string, mode int, listen string, secret string, debug bool) error {
func Setup(baseDir string, workingDir string, tempDir string, mode int, listen string, secret string, debug bool, platformInterface libbox.PlatformInterface) error {
return hcore.Setup(&hcore.SetupRequest{
BasePath: baseDir,
WorkingDir: workingDir,
Expand All @@ -18,7 +18,7 @@ func Setup(baseDir string, workingDir string, tempDir string, mode int, listen s
Debug: debug,
Mode: hcore.SetupMode(mode),
Secret: secret,
})
}, platformInterface)

// return hcore.Start(17078)
}
Expand All @@ -37,11 +37,11 @@ func BuildConfig(configPath string) (string, error) {
// return state, err
// }

func Start(configPath string, configContent string, platformInterface libbox.PlatformInterface) error {
func Start(configPath string, configContent string) error {
_, err := hcore.StartService(&hcore.StartRequest{
ConfigPath: configPath,
ConfigContent: configContent,
}, platformInterface)
})
return err
}

Expand Down
4 changes: 4 additions & 0 deletions v2/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,15 @@ func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOp
case C.TypeCustom:
continue
default:
if opt.Warp.EnableWarp && opt.Warp.Mode == "warp_over_proxy" && out.Tag == "Hiddify Warp ✅" {
continue
}
if !strings.Contains(out.Tag, "§hide§") {
tags = append(tags, out.Tag)
}
out = patchHiddifyWarpFromConfig(out, *opt)
outbounds = append(outbounds, out)

}
}

Expand Down
9 changes: 5 additions & 4 deletions v2/db/hiddify_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ func getDB(name string, readOnly bool) (tmdb.DB, error) {
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
readOnly = false
}

const retryAttempts = 100
const retryDelay = 100 * time.Microsecond

var db tmdb.DB
var err error

defer func() {
if r := recover(); r != nil {
log.Printf("Recovered from panic: %v", r)
}
}()
for i := 0; i < retryAttempts; i++ {
// Set readOnly to true for the first 80 attempts
opts := &opt.Options{ReadOnly: readOnly && i < 80}
Expand All @@ -35,11 +38,9 @@ func getDB(name string, readOnly bool) (tmdb.DB, error) {
if err == nil {
return db, nil
}

log.Printf("Failed attempt %d to initialize the database: %v", i, err)
time.Sleep(retryDelay)
}

return nil, err
}

Expand Down
5 changes: 3 additions & 2 deletions v2/hcore/buildconfighelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ func BuildConfigJson(in *StartRequest) (string, error) {
}

func BuildConfig(in *StartRequest) (*option.Options, error) {
Log(LogLevel_DEBUG, LogType_CORE, "Building Config...")
content := in.ConfigContent

if content == "" {
fileContent, err := os.ReadFile(in.ConfigPath)
if err != nil {
return nil, err
}
content = string(fileContent)
}

Log(LogLevel_DEBUG, LogType_CORE, "Parsing Config")
Log(LogLevel_DEBUG, LogType_CORE, "Parsing Config... ", in.ConfigPath, " content:", content, "-")

parsedContent, err := readOptions(content)
Log(LogLevel_DEBUG, LogType_CORE, "Parsed")
Expand Down
83 changes: 44 additions & 39 deletions v2/hcore/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"net"
"strconv"
"strings"
"time"

"github.com/hiddify/hiddify-core/v2/config"
hcommon "github.com/hiddify/hiddify-core/v2/hcommon"
"github.com/hiddify/hiddify-core/v2/hello"
hutils "github.com/hiddify/hiddify-core/v2/hutils"
Expand Down Expand Up @@ -82,60 +84,59 @@ func StartGrpcServerByMode(listenAddressG string, mode SetupMode) (*grpc.Server,
if hutils.IsPortInUse(uint16(port)) {
return nil, fmt.Errorf("port %s is already in use", portStr)
}

// Fetch the server private key and public key from the database
if grpcServer[mode] != nil {
if _, exists := grpcServer[mode]; exists {
Log(LogLevel_WARNING, LogType_CORE, "grpcServer already started")
return grpcServer[mode], nil
}
table := db.GetTable[hcommon.AppSettings]()
grpcServerPrivateKey, err := table.Get("grpc_server_private_key")
grpcServerPublicKey, err2 := table.Get("grpc_server_public_key")
if err != nil || err2 != nil {
Log(LogLevel_DEBUG, LogType_CORE, fmt.Sprintf("failed to get grpc_server_private_key and grpc_server_public_key from database: %v %v\n", err, err2))
certpair, err = hutils.GenerateCertificatePair()
if err != nil {
Log(LogLevel_ERROR, LogType_CORE, fmt.Sprintf("failed to generate certificate pair: %v", err))

return nil, err
}
table.UpdateInsert(
&hcommon.AppSettings{Id: "grpc_server_public_key", Value: certpair.Certificate},
&hcommon.AppSettings{Id: "grpc_server_private_key", Value: certpair.PrivateKey},
)
if mode == SetupMode_GRPC_BACKGROUND_INSECURE || mode == SetupMode_GRPC_NORMAL_INSECURE {
grpcServer[mode] = grpc.NewServer()
} else {
certpair = &hutils.CertificatePair{
Certificate: grpcServerPublicKey.Value.([]byte),
PrivateKey: grpcServerPrivateKey.Value.([]byte),
table := db.GetTable[hcommon.AppSettings]()
Log(LogLevel_DEBUG, LogType_CORE, table)
grpcServerPrivateKey, err := table.Get("grpc_server_private_key")
grpcServerPublicKey, err2 := table.Get("grpc_server_public_key")
if err != nil || err2 != nil {
Log(LogLevel_DEBUG, LogType_CORE, fmt.Sprintf("failed to get grpc_server_private_key and grpc_server_public_key from database: %v %v\n", err, err2))
certpair, err = hutils.GenerateCertificatePair()
if err != nil {
Log(LogLevel_ERROR, LogType_CORE, fmt.Sprintf("failed to generate certificate pair: %v", err))

return nil, err
}
table.UpdateInsert(
&hcommon.AppSettings{Id: "grpc_server_public_key", Value: certpair.Certificate},
&hcommon.AppSettings{Id: "grpc_server_private_key", Value: certpair.PrivateKey},
)
} else {
certpair = &hutils.CertificatePair{
Certificate: grpcServerPublicKey.Value.([]byte),
PrivateKey: grpcServerPrivateKey.Value.([]byte),
}
}
}

// Load server certificate and private key
serverCert, err := tls.X509KeyPair(certpair.Certificate, certpair.PrivateKey)
if err != nil {
Log(LogLevel_DEBUG, LogType_CORE, fmt.Sprintf("failed to load server certificate and key: %v\n", err))
// Load server certificate and private key
serverCert, err := tls.X509KeyPair(certpair.Certificate, certpair.PrivateKey)
if err != nil {
Log(LogLevel_DEBUG, LogType_CORE, fmt.Sprintf("failed to load server certificate and key: %v\n", err))

return nil, err
}
return nil, err
}

// Create TLS credentials for the gRPC server
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert, // Enforce mutual TLS (mTLS)
ClientCAs: caCertPool, // Client CAs to verify client certificates
}
// Create TLS credentials for the gRPC server
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert, // Enforce mutual TLS (mTLS)
ClientCAs: caCertPool, // Client CAs to verify client certificates
}

// Create a new gRPC server with TLS credentials
creds := credentials.NewTLS(tlsConfig)
if mode == SetupMode_GRPC_BACKGROUND_INSECURE || mode == SetupMode_GRPC_NORMAL_INSECURE {
grpcServer[mode] = grpc.NewServer()
} else {
// Create a new gRPC server with TLS credentials
creds := credentials.NewTLS(tlsConfig)
grpcServer[mode] = grpc.NewServer(grpc.Creds(creds))
}
// Register your gRPC service here
RegisterCoreServer(grpcServer[mode], &CoreService{})
hello.RegisterHelloServer(grpcServer[mode], &hello.HelloService{})

// Listen on the provided address
lis, err := net.Listen("tcp", listenAddressG)
if err != nil {
Expand All @@ -147,6 +148,10 @@ func StartGrpcServerByMode(listenAddressG string, mode SetupMode) (*grpc.Server,

// Run the server in a goroutine
go func() {
defer config.DeferPanicToError("grpcsetup", func(err error) {
Log(LogLevel_FATAL, LogType_CORE, err.Error())
<-time.After(5 * time.Second)
})
if err := grpcServer[mode].Serve(lis); err != nil {
Log(LogLevel_DEBUG, LogType_CORE, fmt.Sprintf("failed to serve: %v\n", err))
}
Expand Down
2 changes: 1 addition & 1 deletion v2/hcore/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func Restart(in *StartRequest) (coreResponse *CoreInfoResponse, err error) {
// <-time.After(250 * time.Millisecond)

libbox.SetMemoryLimit(!in.DisableMemoryLimit)
resp, gErr := StartService(in, nil)
resp, gErr := StartService(in)
return resp, gErr
}
Loading

0 comments on commit fcd117d

Please sign in to comment.