2015-06-03 01:25:37 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-06-12 19:52:27 +01:00
|
|
|
"fmt"
|
2015-11-04 21:53:39 +00:00
|
|
|
"net"
|
2015-06-03 01:25:37 +01:00
|
|
|
"reflect"
|
|
|
|
|
2015-11-20 16:17:33 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
2015-06-03 01:25:37 +01:00
|
|
|
)
|
|
|
|
|
2015-11-04 21:53:39 +00:00
|
|
|
var (
|
|
|
|
privateBlocks []*net.IPNet
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
|
|
|
|
if _, block, err := net.ParseCIDR(b); err == nil {
|
|
|
|
privateBlocks = append(privateBlocks, block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 01:06:51 +00:00
|
|
|
func extractValue(v reflect.Type, d int) *registry.Value {
|
|
|
|
if d == 3 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
arg := ®istry.Value{
|
|
|
|
Name: v.Name(),
|
|
|
|
Type: v.Name(),
|
|
|
|
}
|
|
|
|
|
2015-12-07 16:12:46 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Struct:
|
2015-06-03 01:25:37 +01:00
|
|
|
for i := 0; i < v.NumField(); i++ {
|
2015-12-25 01:06:51 +00:00
|
|
|
val := extractValue(v.Field(i).Type, d+1)
|
|
|
|
if val == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
val.Name = v.Field(i).Name
|
|
|
|
arg.Values = append(arg.Values, val)
|
|
|
|
}
|
2015-12-07 16:12:46 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
p := v.Elem()
|
|
|
|
if p.Kind() == reflect.Ptr {
|
|
|
|
p = p.Elem()
|
|
|
|
}
|
|
|
|
arg.Type = "[]" + p.Name()
|
2015-12-25 01:06:51 +00:00
|
|
|
val := extractValue(v.Elem(), d+1)
|
|
|
|
if val != nil {
|
|
|
|
arg.Values = append(arg.Values, val)
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractEndpoint(method reflect.Method) *registry.Endpoint {
|
|
|
|
if method.PkgPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rspType, reqType reflect.Type
|
2015-06-12 19:52:27 +01:00
|
|
|
var stream bool
|
2015-06-03 01:25:37 +01:00
|
|
|
mt := method.Type
|
|
|
|
|
|
|
|
switch mt.NumIn() {
|
|
|
|
case 3:
|
|
|
|
reqType = mt.In(1)
|
|
|
|
rspType = mt.In(2)
|
|
|
|
case 4:
|
|
|
|
reqType = mt.In(2)
|
|
|
|
rspType = mt.In(3)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:52:08 +01:00
|
|
|
// are we dealing with a stream?
|
|
|
|
switch rspType.Kind() {
|
|
|
|
case reflect.Func, reflect.Interface:
|
2015-06-12 19:52:27 +01:00
|
|
|
stream = true
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
|
2015-12-25 01:06:51 +00:00
|
|
|
request := extractValue(reqType, 0)
|
|
|
|
response := extractValue(rspType, 0)
|
2015-06-03 01:25:37 +01:00
|
|
|
|
|
|
|
return ®istry.Endpoint{
|
|
|
|
Name: method.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
2015-06-12 19:52:27 +01:00
|
|
|
Metadata: map[string]string{
|
|
|
|
"stream": fmt.Sprintf("%v", stream),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractSubValue(typ reflect.Type) *registry.Value {
|
|
|
|
var reqType reflect.Type
|
|
|
|
switch typ.NumIn() {
|
|
|
|
case 1:
|
|
|
|
reqType = typ.In(0)
|
|
|
|
case 2:
|
|
|
|
reqType = typ.In(1)
|
2015-12-03 00:49:58 +00:00
|
|
|
case 3:
|
|
|
|
reqType = typ.In(2)
|
2015-06-12 19:52:27 +01:00
|
|
|
default:
|
|
|
|
return nil
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
2015-12-25 01:06:51 +00:00
|
|
|
return extractValue(reqType, 0)
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
2015-11-04 21:53:39 +00:00
|
|
|
|
|
|
|
func extractAddress(addr string) (string, error) {
|
|
|
|
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]") {
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to get interface addresses! Err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipAddr []byte
|
|
|
|
|
|
|
|
for _, rawAddr := range addrs {
|
|
|
|
var ip net.IP
|
|
|
|
switch addr := rawAddr.(type) {
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = addr.IP
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isPrivateIP(ip.String()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ipAddr = ip
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipAddr == nil {
|
|
|
|
return "", fmt.Errorf("No private IP address found, and explicit IP not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.IP(ipAddr).String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPrivateIP(ipAddr string) bool {
|
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
for _, priv := range privateBlocks {
|
|
|
|
if priv.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|