Browse Source

optimize code struct

pull/15/head
Jason 5 years ago
parent
commit
10d8bdc2e6
  1. 9
      cmd/main.go
  2. 13
      cmd/main_fakedns.go
  3. 6
      cmd/main_stats.go

9
cmd/main.go

@ -11,14 +11,15 @@ import (
"syscall"
"time"
"github.com/xjasonlyu/tun2socks/common/dns"
"github.com/xjasonlyu/tun2socks/common/log"
"github.com/xjasonlyu/tun2socks/common/stats"
"github.com/xjasonlyu/tun2socks/core"
"github.com/xjasonlyu/tun2socks/filter"
"github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/tun"
D "github.com/xjasonlyu/tun2socks/common/dns"
S "github.com/xjasonlyu/tun2socks/common/stats"
// init logger
_ "github.com/xjasonlyu/tun2socks/common/log/simple"
)
@ -32,8 +33,8 @@ var (
args = new(CmdArgs)
postFlagsInitFn []func()
fakeDNS dns.FakeDNS
sessionStater stats.SessionStater
fakeDNS D.FakeDNS
sessionStater S.SessionStater
)
type CmdArgs struct {

13
cmd/main_fakedns.go

@ -6,6 +6,7 @@ import (
"flag"
"github.com/xjasonlyu/tun2socks/common/dns/fakedns"
"github.com/xjasonlyu/tun2socks/common/log"
)
func init() {
@ -17,16 +18,16 @@ func init() {
addPostFlagsInitFn(func() {
if *args.EnableFakeDNS {
fakeDNSServer, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDNSHosts, *args.DNSCacheSize)
fakeDNS, err := fakedns.NewServer(*args.FakeIPRange, *args.FakeDNSHosts, *args.DNSCacheSize)
if err != nil {
panic("create fake dns server error")
log.Fatalf("create fake dns server failed: %v", err)
}
// Set fakeDNS variables
fakedns.ServeAddr = *args.FakeDNSAddr
if err := fakeDNSServer.Start(); err != nil {
panic("cannot start fake dns server")
}
fakeDNS = fakeDNSServer
// Start fakeDNS server
fakeDNS.Start()
} else {
fakeDNS = nil
}

6
cmd/main_stats.go

@ -9,18 +9,18 @@ import (
)
func init() {
args.Stats = flag.Bool("stats", false, "Enable statistics")
args.Stats = flag.Bool("stats", false, "Enable session statistics")
args.StatsAddr = flag.String("statsAddr", "localhost:6001", "Listen address of stats, open in your browser to view statistics")
addPostFlagsInitFn(func() {
if *args.Stats {
sessionStater = session.NewSimpleSessionStater()
// stats variables
// Set stats variables
session.ServeAddr = *args.StatsAddr
session.StatsVersion = version
// start session stater
// Start session stater
sessionStater.Start()
} else {
sessionStater = nil

Loading…
Cancel
Save