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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Package rpc contains wrapper around RPC client ONLY for internal usage.
// Should be in sync with the RPC plugin
package rpc
import (
"errors"
"fmt"
"net"
"net/rpc"
"os"
"strings"
goridgeRpc "github.com/roadrunner-server/goridge/v3/pkg/rpc"
rpcPlugin "github.com/roadrunner-server/rpc/v5"
"github.com/spf13/viper"
)
const (
rpcKey string = "rpc.listen"
// default envs
envDefault = ":-"
)
// NewClient creates client ONLY for internal usage (communication between our application with RR side).
// Client will be connected to the RPC.
func NewClient(cfg string, flags []string) (*rpc.Client, error) {
v := viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetConfigFile(cfg)
err := v.ReadInConfig()
if err != nil {
return nil, err
}
// automatically inject ENV variables using ${ENV} pattern
expandEnvViper(v)
// override config Flags
if len(flags) > 0 {
for _, f := range flags {
key, val, errP := parseFlag(f)
if errP != nil {
return nil, errP
}
v.Set(key, val)
}
}
// rpc.listen might be set by the -o flags or env variable
if !v.IsSet(rpcPlugin.PluginName) {
return nil, errors.New("rpc service not specified in the configuration. Tip: add\n rpc:\n\r listen: rr_rpc_address")
}
conn, err := Dialer(v.GetString(rpcKey))
if err != nil {
return nil, err
}
return rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)), nil
}
// Dialer creates rpc socket Dialer.
func Dialer(addr string) (net.Conn, error) {
dsn := strings.Split(addr, "://")
if len(dsn) != 2 {
return nil, errors.New("invalid socket DSN (tcp://:6001, unix://file.sock)")
}
return net.Dial(dsn[0], dsn[1])
}
func parseFlag(flag string) (string, string, error) {
if !strings.Contains(flag, "=") {
return "", "", fmt.Errorf("invalid flag `%s`", flag)
}
parts := strings.SplitN(strings.TrimLeft(flag, " \"'`"), "=", 2)
if len(parts) < 2 {
return "", "", errors.New("usage: -o key=value")
}
if parts[0] == "" {
return "", "", errors.New("key should not be empty")
}
if parts[1] == "" {
return "", "", errors.New("value should not be empty")
}
return strings.Trim(parts[0], " \n\t"), parseValue(strings.Trim(parts[1], " \n\t")), nil
}
func parseValue(value string) string {
escape := []rune(value)[0]
if escape == '"' || escape == '\'' || escape == '`' {
value = strings.Trim(value, string(escape))
value = strings.ReplaceAll(value, fmt.Sprintf("\\%s", string(escape)), string(escape))
}
return value
}
// ExpandVal replaces ${var} or $var in the string based on the mapping function.
// For example, os.ExpandEnv(s) is equivalent to os.Expand(s, os.Getenv).
func ExpandVal(s string, mapping func(string) string) string {
var buf []byte
// ${} is all ASCII, so bytes are fine for this operation.
i := 0
for j := 0; j < len(s); j++ {
if s[j] == '$' && j+1 < len(s) {
if buf == nil {
buf = make([]byte, 0, 2*len(s))
}
buf = append(buf, s[i:j]...)
name, w := getShellName(s[j+1:])
if name == "" && w > 0 { //nolint:revive
// Encountered invalid syntax; eat the
// characters.
} else if name == "" {
// Valid syntax, but $ was not followed by a
// name. Leave the dollar character untouched.
buf = append(buf, s[j])
// parse default syntax
} else if idx := strings.Index(s, envDefault); idx != -1 {
// ${key:=default} or ${key:-val}
substr := strings.Split(name, envDefault)
if len(substr) != 2 {
return ""
}
key := substr[0]
defaultVal := substr[1]
res := mapping(key)
if res == "" {
res = defaultVal
}
buf = append(buf, res...)
} else {
buf = append(buf, mapping(name)...)
}
j += w
i = j + 1
}
}
if buf == nil {
return s
}
return string(buf) + s[i:]
}
// getShellName returns the name that begins the string and the number of bytes
// consumed to extract it. If the name is enclosed in {}, it's part of a ${}
// expansion and two more bytes are needed than the length of the name.
func getShellName(s string) (string, int) {
switch {
case s[0] == '{':
if len(s) > 2 && isShellSpecialVar(s[1]) && s[2] == '}' {
return s[1:2], 3
}
// Scan to closing brace
for i := 1; i < len(s); i++ {
if s[i] == '}' {
if i == 1 {
return "", 2 // Bad syntax; eat "${}"
}
return s[1:i], i + 1
}
}
return "", 1 // Bad syntax; eat "${"
case isShellSpecialVar(s[0]):
return s[0:1], 1
}
// Scan alphanumerics.
var i int
for i = 0; i < len(s) && isAlphaNum(s[i]); i++ { //nolint:revive
}
return s[:i], i
}
func expandEnvViper(v *viper.Viper) {
for _, key := range v.AllKeys() {
val := v.Get(key)
switch t := val.(type) {
case string:
// for string expand it
v.Set(key, parseEnvDefault(t))
case []any:
// for slice -> check if it's a slice of strings
strArr := make([]string, 0, len(t))
for i := 0; i < len(t); i++ {
if valStr, ok := t[i].(string); ok {
strArr = append(strArr, parseEnvDefault(valStr))
continue
}
v.Set(key, val)
}
// we should set the whole array
if len(strArr) > 0 {
v.Set(key, strArr)
}
default:
v.Set(key, val)
}
}
}
// isShellSpecialVar reports whether the character identifies a special
// shell variable such as $*.
func isShellSpecialVar(c uint8) bool {
switch c {
case '*', '#', '$', '@', '!', '?', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return true
}
return false
}
// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore.
func isAlphaNum(c uint8) bool {
return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
}
func parseEnvDefault(val string) string {
// tcp://127.0.0.1:${RPC_PORT:-36643}
// for envs like this, part would be tcp://127.0.0.1:
return ExpandVal(val, os.Getenv)
}
|