This repository has been archived by the owner on Apr 10, 2023. It is now read-only.
/
proxy.go
99 lines (87 loc) · 2.07 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"github.com/blockchainstamp/fdlimit"
bp "github.com/blockchainstamp/go-mail-proxy"
"github.com/blockchainstamp/go-mail-proxy/proxy_v1"
"github.com/blockchainstamp/go-mail-proxy/utils"
"github.com/spf13/cobra"
"math/rand"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
)
var (
proxyCmd = &cobra.Command{
Use: "proxy",
Short: "start proxy service",
Long: `TODO::`,
Run: proxy,
}
sigCh = make(chan os.Signal, 1)
configPath string
walletAuth string
)
func init() {
proxyCmd.Flags().StringVarP(&configPath, "conf",
"c", "proxy.json", "configure file path --conf||-c [CONFIG_FILE_PATH]")
proxyCmd.Flags().StringVarP(&walletAuth, "auth",
"a", "", "--auth||-a [Password Of Current Stamp Wallet]")
rootCmd.AddCommand(proxyCmd)
}
func initSystem() error {
if err := os.Setenv("GODEBUG", "netdns=go"); err != nil {
return err
}
runtime.GOMAXPROCS(runtime.NumCPU())
rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
limit, err := fdlimit.Maximum()
if err != nil {
return fmt.Errorf("failed to retrieve file descriptor allowance:%s", err)
}
_, err = fdlimit.Raise(uint64(limit))
if err != nil {
return fmt.Errorf("failed to raise file descriptor allowance:%s", err)
}
return nil
}
func waitSignal() {
pid := strconv.Itoa(os.Getpid())
fmt.Printf("\n>>>>>>>>>>proxy start at pid(%s)<<<<<<<<<<\n", pid)
signal.Notify(sigCh,
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGINT,
syscall.SIGKILL,
syscall.SIGUSR1,
os.Kill,
)
//TODO:: process system signal
for sig := range sigCh {
fmt.Printf("\n>>>>>>>>>>proxy[%s] finished(%s)<<<<<<<<<<\n", pid, sig)
return
}
}
func proxy(cmd *cobra.Command, args []string) {
if err := initSystem(); err != nil {
panic(err)
}
cnf := &proxy_v1.Config{}
if err := utils.ReadJsonFile(configPath, cnf); err != nil {
panic(err)
}
if err := bp.Inst().InitByConf(cnf, walletAuth); err != nil {
panic(err)
}
if err := bp.Inst().Start(); err != nil {
panic(err)
}
waitSignal()
if err := bp.Inst().ShutDown(); err != nil {
fmt.Println(err)
}
}