Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
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
package restfulapi
 
import (
    "context"
    "net"
    "sync"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/features"
    feature_stats "github.com/v2fly/v2ray-core/v5/features/stats"
)
 
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
 
type restfulService struct {
    listener net.Listener
    config   *Config
    access   sync.Mutex
 
    stats feature_stats.Manager
 
    ctx context.Context
}
 
func (rs *restfulService) Type() interface{} {
    return (*struct{})(nil)
}
 
func (rs *restfulService) Start() error {
    defer rs.access.Unlock()
    rs.access.Lock()
    return rs.start()
}
 
func (rs *restfulService) Close() error {
    defer rs.access.Unlock()
    rs.access.Lock()
    if rs.listener != nil {
        return rs.listener.Close()
    }
    return nil
}
 
func (rs *restfulService) init(config *Config, stats feature_stats.Manager) {
    rs.stats = stats
    rs.config = config
}
 
func newRestfulService(ctx context.Context, config *Config) (features.Feature, error) {
    r := new(restfulService)
    r.ctx = ctx
    if err := core.RequireFeatures(ctx, func(stats feature_stats.Manager) {
        r.init(config, stats)
    }); err != nil {
        return nil, err
    }
    return r, nil
}