12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- // netif implements helper functions to read network interfaces.
- // warning: ONLY suport standard Linux interface config.
- package server
- import (
- "net"
- "strings"
- )
- var (
- InternalIP string
- ExternalIP string
- )
- const (
- confInternalIP = "internal"
- confExternalIP = "external"
- )
- //to see if an IP is internal ip
- func isInternalIP(ip string) bool {
- if ip == "127.0.0.1" {
- return true
- }
- ipSplit := strings.Split(ip, ".")
- if ipSplit[0] == "10" {
- return true
- }
- if ipSplit[0] == "172" && ipSplit[1] >= "16" && ipSplit[1] <= "31" {
- return true
- }
- if ipSplit[0] == "192" && ipSplit[1] == "168" {
- return true
- }
- return false
- }
- //read server IP
- func readNetInterfaces() {
- addrs, err := net.InterfaceAddrs()
- if err != nil {
- return
- }
- for _, inter := range addrs {
- if len(inter.String()) == 0 {
- continue
- }
- ip := strings.Split(inter.String(), "/")[0]
- if isInternalIP(ip) {
- InternalIP = ip
- } else {
- ExternalIP = ip
- }
- }
- if *confExIp != "" {
- ExternalIP = *confExIp
- }
- return
- }
- // fix host ip with "internal:port" or "external:port" format
- func fixHostIp(address string) (*addr, error) {
- var ex, in string
- if *confExIp != "" {
- if strings.Contains(address, confExternalIP) {
- in = strings.Replace(address, confExternalIP, "0.0.0.0", -1)
- }
- ex = strings.Replace(address, confExternalIP, *confExIp, -1)
- } else {
- if strings.Contains(address, confInternalIP) {
- in = strings.Replace(address, confInternalIP, InternalIP, -1)
- ex = in
- } else {
- in = address
- ex = address
- }
- }
- return &addr{
- externalIp: ex,
- internalIp: in,
- }, nil
- }
|