This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
165 lines (134 loc) · 3.35 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// go-pluginserver is a standalone RPC server that runs
// Go plugins for Kong.
package main
import (
"flag"
"fmt"
"github.com/ugorji/go/codec"
"log"
"net"
"net/rpc"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
)
var version = "development"
/* flags */
var (
kongPrefix = flag.String("kong-prefix", "/usr/local/kong", "Kong prefix path (specified by the -p argument commonly used in the kong cli)")
dump = flag.String("dump-plugin-info", "", "Dump info about `plugin` as a MessagePack object")
dumpAllPlugins = flag.Bool("dump-all-plugins", false, "Dump info about all available plugins")
pluginsDir = flag.String("plugins-directory", "", "Set directory `path` where to search plugins")
showVersion = flag.Bool("version", false, "Print binary and runtime version")
)
var socket string
func init() {
flag.Parse()
socket = *kongPrefix + "/" + "go_pluginserver.sock"
if *kongPrefix == "" && *dump == "" {
flag.Usage()
os.Exit(2)
}
}
func printVersion() {
fmt.Printf("Version: %s\nRuntime Version: %s\n", version, runtime.Version())
}
func dumpInfo() {
s := newServer()
info := PluginInfo{}
err := s.GetPluginInfo(*dump, &info)
if err != nil {
log.Printf("%s", err)
}
var handle codec.MsgpackHandle
handle.ReaderBufferSize = 4096
handle.WriterBufferSize = 4096
handle.RawToString = true
handle.MapType = reflect.TypeOf(map[string]interface{}(nil))
enc := codec.NewEncoder(os.Stdout, &handle)
_ = enc.Encode(info)
}
func dumpAll() {
s := newServer()
pluginPaths, err := filepath.Glob(path.Join(s.pluginsDir, "/*.so"))
if err != nil {
log.Printf("can't get plugin names from %s: %s", s.pluginsDir, err)
return
}
infos := make([]PluginInfo, len(pluginPaths))
for i, pluginPath := range pluginPaths {
pluginName := strings.TrimSuffix(path.Base(pluginPath), ".so")
err = s.GetPluginInfo(pluginName, &infos[i])
if err != nil {
log.Printf("can't load Plugin %s: %s", pluginName, err)
continue
}
}
var handle codec.JsonHandle
enc := codec.NewEncoder(os.Stdout, &handle)
_ = enc.Encode(infos)
}
func runServer(listener net.Listener) {
var handle codec.MsgpackHandle
handle.ReaderBufferSize = 4096
handle.WriterBufferSize = 4096
handle.RawToString = true
handle.MapType = reflect.TypeOf(map[string]interface{}(nil))
for {
conn, err := listener.Accept()
if err != nil {
return
}
enc := codec.NewEncoder(conn, &handle)
_ = enc.Encode([]interface{}{2, "serverPid", os.Getpid()})
rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, &handle)
go rpc.ServeCodec(rpcCodec)
}
}
func startServer() {
err := os.Remove(socket)
if err != nil && !os.IsNotExist(err) {
log.Printf(`removing "%s": %s`, kongPrefix, err)
return
}
listener, err := net.Listen("unix", socket)
if err != nil {
log.Printf(`listen("%s"): %s`, socket, err)
return
}
rpc.RegisterName("plugin", newServer())
runServer(listener)
}
func isParentAlive() bool {
return os.Getppid() != 1 // assume ppid 1 means process was adopted by init
}
func main() {
if *showVersion == true {
printVersion()
os.Exit(0)
}
if *dump != "" {
dumpInfo()
os.Exit(0)
}
if *dumpAllPlugins {
dumpAll()
os.Exit(0)
}
if socket != "" {
go func() {
for {
if !isParentAlive() {
log.Printf("Kong exited; shutting down...")
os.Exit(0)
}
time.Sleep(1 * time.Second)
}
}()
startServer()
}
}