Skip to content

Commit b95593b

Browse files
committed
chore: move tools to pkg
1 parent 7907429 commit b95593b

9 files changed

Lines changed: 23 additions & 31 deletions

File tree

backend/xray/xray_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111

1212
"github.com/pasarguard/node/common"
1313
"github.com/pasarguard/node/config"
14-
"github.com/pasarguard/node/tools"
14+
"github.com/pasarguard/node/pkg/fsutil"
15+
"github.com/pasarguard/node/pkg/netutil"
1516
)
1617

1718
var (
@@ -22,7 +23,7 @@ var (
2223
)
2324

2425
func TestXrayBackend(t *testing.T) {
25-
xrayFile, err := tools.ReadFileAsString(jsonFile)
26+
xrayFile, err := fsutil.ReadFileAsString(jsonFile)
2627
if err != nil {
2728
t.Fatal(err)
2829
}
@@ -99,7 +100,7 @@ func TestXrayBackend(t *testing.T) {
99100
LogBufferSize: 1000,
100101
}
101102

102-
back, err := New(context.Background(), newConfig, []*common.User{user, user2}, tools.FindFreePort(), cfg)
103+
back, err := New(context.Background(), newConfig, []*common.User{user, user2}, netutil.FindFreePort(), cfg)
103104
if err != nil {
104105
t.Fatal(err)
105106
}

controller/controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"github.com/pasarguard/node/backend/xray"
1515
"github.com/pasarguard/node/common"
1616
"github.com/pasarguard/node/config"
17-
"github.com/pasarguard/node/tools"
17+
"github.com/pasarguard/node/pkg/netutil"
18+
"github.com/pasarguard/node/pkg/sysstats"
1819
)
1920

2021
const NodeVersion = "0.3.1"
@@ -38,7 +39,7 @@ func New(cfg *config.Config) *Controller {
3839
_, cancel := context.WithCancel(context.Background())
3940
return &Controller{
4041
cfg: cfg,
41-
apiPort: tools.FindFreePort(),
42+
apiPort: netutil.FindFreePort(),
4243
cancelFunc: cancel,
4344
}
4445
}
@@ -80,7 +81,7 @@ func (c *Controller) Disconnect() {
8081
defer c.mu.Unlock()
8182

8283
c.backend = nil
83-
c.apiPort = tools.FindFreePort()
84+
c.apiPort = netutil.FindFreePort()
8485
c.clientIP = ""
8586
}
8687

@@ -162,7 +163,7 @@ func (c *Controller) recordSystemStats(ctx context.Context) {
162163
case <-ctx.Done():
163164
return
164165
default:
165-
stats, err := tools.GetSystemStats()
166+
stats, err := sysstats.GetSystemStats()
166167
if err != nil {
167168
log.Printf("Failed to get system stats: %v", err)
168169
} else {

controller/rest/rest_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/pasarguard/node/common"
2121
"github.com/pasarguard/node/config"
2222
"github.com/pasarguard/node/controller"
23-
"github.com/pasarguard/node/tools"
23+
"github.com/pasarguard/node/pkg/tlsutil"
2424
)
2525

2626
var (
@@ -50,7 +50,7 @@ func TestMain(m *testing.M) {
5050
// Setup
5151
cfg := config.NewTestConfig(generatedConfigPath, apiKey)
5252

53-
tlsConfig, err := tools.LoadTLSCredentials(sslCertFile, sslKeyFile)
53+
tlsConfig, err := tlsutil.LoadTLSCredentials(sslCertFile, sslKeyFile)
5454
if err != nil {
5555
log.Fatalf("Failed to load TLS credentials: %v", err)
5656
}
@@ -60,11 +60,11 @@ func TestMain(m *testing.M) {
6060
log.Fatalf("Failed to start HTTP listener: %v", err)
6161
}
6262

63-
certPool, err := tools.LoadClientPool(sslCertFile)
63+
certPool, err := tlsutil.LoadClientPool(sslCertFile)
6464
if err != nil {
6565
log.Fatalf("Failed to load client pool: %v", err)
6666
}
67-
client := tools.CreateHTTPClient(certPool, nodeHost)
67+
client := tlsutil.CreateHTTPClient(certPool, nodeHost)
6868

6969
url := fmt.Sprintf("https://%s", addr)
7070

controller/rpc/rpc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/pasarguard/node/common"
2020
"github.com/pasarguard/node/config"
2121
"github.com/pasarguard/node/controller"
22-
"github.com/pasarguard/node/tools"
22+
"github.com/pasarguard/node/pkg/tlsutil"
2323
)
2424

2525
var (
@@ -48,7 +48,7 @@ func TestMain(m *testing.M) {
4848
// Setup
4949
cfg := config.NewTestConfig(generatedConfigPath, apiKey)
5050

51-
tlsConfig, err := tools.LoadTLSCredentials(sslCertFile, sslKeyFile)
51+
tlsConfig, err := tlsutil.LoadTLSCredentials(sslCertFile, sslKeyFile)
5252
if err != nil {
5353
log.Fatalf("Failed to load TLS credentials: %v", err)
5454
}
@@ -58,7 +58,7 @@ func TestMain(m *testing.M) {
5858
log.Fatalf("Failed to start gRPC listener: %v", err)
5959
}
6060

61-
certPool, err := tools.LoadClientPool(sslCertFile)
61+
certPool, err := tlsutil.LoadClientPool(sslCertFile)
6262
if err != nil {
6363
log.Fatalf("Failed to load client pool: %v", err)
6464
}

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/pasarguard/node/controller"
1414
"github.com/pasarguard/node/controller/rest"
1515
"github.com/pasarguard/node/controller/rpc"
16-
"github.com/pasarguard/node/tools"
16+
"github.com/pasarguard/node/pkg/tlsutil"
1717
)
1818

1919
func main() {
@@ -24,7 +24,7 @@ func main() {
2424

2525
addr := fmt.Sprintf("%s:%d", cfg.NodeHost, cfg.ServicePort)
2626

27-
tlsConfig, err := tools.LoadTLSCredentials(cfg.SslCertFile, cfg.SslKeyFile)
27+
tlsConfig, err := tlsutil.LoadTLSCredentials(cfg.SslCertFile, cfg.SslKeyFile)
2828
if err != nil {
2929
log.Fatal(err)
3030
}

tools/file.go renamed to pkg/fsutil/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools
1+
package fsutil
22

33
import (
44
"os"

tools/port.go renamed to pkg/netutil/port.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools
1+
package netutil
22

33
import (
44
"fmt"

tools/sys.go renamed to pkg/sysstats/sys.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools
1+
package sysstats
22

33
import (
44
"time"
@@ -45,40 +45,31 @@ func GetSystemStats() (*common.SystemStatsResponse, error) {
4545
}
4646

4747
// getBandwidthSpeed returns the aggregate incoming (rx) and outgoing (tx)
48-
// bandwidth in bytes per second, sampled over a 1second interval.
48+
// bandwidth in bytes per second, sampled over a 1-second interval.
4949
// Loopback interface (lo) is excluded from the calculation.
5050
func getBandwidthSpeed() (uint64, uint64, error) {
51-
// 1) First snapshot with timestamp
5251
first, err := net.IOCounters(true)
5352
if err != nil {
5453
return 0, 0, err
5554
}
5655

57-
// 2) Wait one second
5856
time.Sleep(1 * time.Second)
5957

60-
// 3) Second snapshot with timestamp
6158
second, err := net.IOCounters(true)
6259
if err != nil {
6360
return 0, 0, err
6461
}
6562

66-
// 4) Build a map from interface name → first snapshot
67-
// Skip loopback interface
6863
prev := make(map[string]net.IOCountersStat, len(first))
6964
for _, c := range first {
70-
// Skip loopback interface
7165
if c.Name == "lo" {
7266
continue
7367
}
7468
prev[c.Name] = c
7569
}
7670

77-
// 5) Compute deltas and sum across all interfaces
78-
// Skip loopback interface
7971
var totalRxBytes, totalTxBytes uint64
8072
for _, c := range second {
81-
// Skip loopback interface
8273
if c.Name == "lo" {
8374
continue
8475
}
@@ -88,6 +79,5 @@ func getBandwidthSpeed() (uint64, uint64, error) {
8879
}
8980
}
9081

91-
// 6) Return the calculated rates (bytes per second)
9282
return totalRxBytes, totalTxBytes, nil
9383
}

tools/tls.go renamed to pkg/tlsutil/tls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tools
1+
package tlsutil
22

33
import (
44
"crypto/tls"

0 commit comments

Comments
 (0)