-
Notifications
You must be signed in to change notification settings - Fork 78
/
core.go
94 lines (88 loc) · 2.34 KB
/
core.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
package core
import (
"context"
"errors"
"fmt"
"log"
"net"
"strconv"
"time"
"github.com/xxf098/lite-proxy/config"
_ "github.com/xxf098/lite-proxy/config"
"github.com/xxf098/lite-proxy/dns"
"github.com/xxf098/lite-proxy/outbound"
"github.com/xxf098/lite-proxy/proxy"
"github.com/xxf098/lite-proxy/request"
"github.com/xxf098/lite-proxy/transport/resolver"
"github.com/xxf098/lite-proxy/tunnel"
"github.com/xxf098/lite-proxy/tunnel/adapter"
"github.com/xxf098/lite-proxy/tunnel/http"
"github.com/xxf098/lite-proxy/tunnel/socks"
"github.com/xxf098/lite-proxy/utils"
)
func StartInstance(c Config) (*proxy.Proxy, error) {
ctx := context.Background()
ctx = context.WithValue(ctx, "LocalHost", c.LocalHost)
ctx = context.WithValue(ctx, "LocalPort", c.LocalPort)
adapterServer, err := adapter.NewServer(ctx, nil)
if err != nil {
return nil, err
}
httpServer, err := http.NewServer(ctx, adapterServer)
if err != nil {
return nil, err
}
socksServer, err := socks.NewServer(ctx, adapterServer)
if err != nil {
return nil, err
}
sources := []tunnel.Server{httpServer, socksServer}
sink, err := createSink(ctx, c.Link)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
go func(link string) {
if c.Ping < 1 {
return
}
if cfg, err := config.Link2Config(c.Link); err == nil {
opt := request.PingOption{
Attempts: c.Ping,
TimeOut: 1200 * time.Millisecond,
}
info := fmt.Sprintf("%s %s", cfg.Remarks, net.JoinHostPort(cfg.Server, strconv.Itoa(cfg.Port)))
if elapse, err := request.PingLinkInternal(link, opt); err == nil {
info = fmt.Sprintf("%s \033[32m%dms\033[0m", info, elapse)
} else {
info = fmt.Sprintf("\033[31m%s\033[0m", err.Error())
}
log.Print(info)
}
}(c.Link)
setDefaultResolver()
p := proxy.NewProxy(ctx, cancel, sources, sink)
return p, nil
}
func createSink(ctx context.Context, link string) (tunnel.Client, error) {
var d outbound.Dialer
matches, err := utils.CheckLink(link)
if err != nil {
return nil, err
}
creator, err := outbound.GetDialerCreator(matches[1])
if err != nil {
return nil, err
}
d, err = creator(link)
if err != nil {
return nil, err
}
if d != nil {
return proxy.NewClient(ctx, d), nil
}
return nil, errors.New("not supported link")
}
func setDefaultResolver() {
resolver.DefaultResolver = dns.DefaultResolver()
}