mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-04-28 14:29:19 +08:00
* fix: minor log typo * fix: #2895 no need to specify listen port in config file, just use system random * drop useless code
This commit is contained in:
@@ -46,8 +46,7 @@ func (s *Server) InitServer(ctx context.Context, config *Config, disCov discover
|
||||
|
||||
func (s *Server) Start(ctx context.Context, index int, conf *Config) error {
|
||||
return startrpc.Start(ctx, &conf.Discovery, &conf.MsgGateway.Prometheus, conf.MsgGateway.ListenIP,
|
||||
conf.MsgGateway.RPC.RegisterIP,
|
||||
conf.MsgGateway.RPC.Ports, index,
|
||||
index,
|
||||
conf.Share.RpcRegisterName.MessageGateway,
|
||||
&conf.Share,
|
||||
conf,
|
||||
@@ -57,7 +56,7 @@ func (s *Server) Start(ctx context.Context, index int, conf *Config) error {
|
||||
|
||||
type Server struct {
|
||||
msggateway.UnimplementedMsgGatewayServer
|
||||
rpcPort int
|
||||
|
||||
LongConnServer LongConnServer
|
||||
config *Config
|
||||
pushTerminal map[int]struct{}
|
||||
@@ -70,9 +69,8 @@ func (s *Server) SetLongConnServer(LongConnServer LongConnServer) {
|
||||
s.LongConnServer = LongConnServer
|
||||
}
|
||||
|
||||
func NewServer(rpcPort int, longConnServer LongConnServer, conf *Config, ready func(srv *Server) error) *Server {
|
||||
func NewServer(longConnServer LongConnServer, conf *Config, ready func(srv *Server) error) *Server {
|
||||
s := &Server{
|
||||
rpcPort: rpcPort,
|
||||
LongConnServer: longConnServer,
|
||||
pushTerminal: make(map[int]struct{}),
|
||||
config: conf,
|
||||
|
||||
@@ -35,16 +35,13 @@ type Config struct {
|
||||
|
||||
// Start run ws server.
|
||||
func Start(ctx context.Context, index int, conf *Config) error {
|
||||
log.CInfo(ctx, "MSG-GATEWAY server is initializing", "rpcPorts", conf.MsgGateway.RPC.Ports,
|
||||
log.CInfo(ctx, "MSG-GATEWAY server is initializing",
|
||||
"wsPort", conf.MsgGateway.LongConnSvr.Ports, "prometheusPorts", conf.MsgGateway.Prometheus.Ports)
|
||||
wsPort, err := datautil.GetElemByIndex(conf.MsgGateway.LongConnSvr.Ports, index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rpcPort, err := datautil.GetElemByIndex(conf.MsgGateway.RPC.Ports, index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rdb, err := redisutil.NewRedisClient(ctx, conf.RedisConfig.Build())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -57,7 +54,7 @@ func Start(ctx context.Context, index int, conf *Config) error {
|
||||
WithMessageMaxMsgLength(conf.MsgGateway.LongConnSvr.WebsocketMaxMsgLen),
|
||||
)
|
||||
|
||||
hubServer := NewServer(rpcPort, longServer, conf, func(srv *Server) error {
|
||||
hubServer := NewServer(longServer, conf, func(srv *Server) error {
|
||||
longServer.online, _ = rpccache.NewOnlineCache(srv.userRcp, nil, rdb, false, longServer.subscriberUserOnlineStatusChanges)
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
// addr provides functions to retrieve local IP addresses from device interfaces.
|
||||
package addr
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrIPNotFound no IP address found, and explicit IP not provided.
|
||||
ErrIPNotFound = errors.New("no IP address found, and explicit IP not provided")
|
||||
)
|
||||
|
||||
// IsLocal checks whether an IP belongs to one of the device's interfaces.
|
||||
func IsLocal(addr string) bool {
|
||||
// Extract the host
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err == nil {
|
||||
addr = host
|
||||
}
|
||||
|
||||
if addr == "localhost" {
|
||||
return true
|
||||
}
|
||||
|
||||
// Check against all local ips
|
||||
for _, ip := range IPs() {
|
||||
if addr == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Extract returns a valid IP address. If the address provided is a valid
|
||||
// address, it will be returned directly. Otherwise, the available interfaces
|
||||
// will be iterated over to find an IP address, preferably private.
|
||||
func Extract(addr string) (string, error) {
|
||||
// if addr is already specified then it's directly returned
|
||||
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
var (
|
||||
addrs []net.Addr
|
||||
loAddrs []net.Addr
|
||||
)
|
||||
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to get interfaces")
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
ifaceAddrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
// ignore error, interface can disappear from system
|
||||
continue
|
||||
}
|
||||
|
||||
if iface.Flags&net.FlagLoopback != 0 {
|
||||
loAddrs = append(loAddrs, ifaceAddrs...)
|
||||
continue
|
||||
}
|
||||
|
||||
addrs = append(addrs, ifaceAddrs...)
|
||||
}
|
||||
|
||||
// Add loopback addresses to the end of the list
|
||||
addrs = append(addrs, loAddrs...)
|
||||
|
||||
// Try to find private IP in list, public IP otherwise
|
||||
ip, err := findIP(addrs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ip.String(), nil
|
||||
}
|
||||
|
||||
// IPs returns all available interface IP addresses.
|
||||
func IPs() []string {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ipAddrs []string
|
||||
|
||||
for _, i := range ifaces {
|
||||
addrs, err := i.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
switch v := addr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
|
||||
if ip == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ipAddrs = append(ipAddrs, ip.String())
|
||||
}
|
||||
}
|
||||
|
||||
return ipAddrs
|
||||
}
|
||||
|
||||
// findIP will return the first private IP available in the list.
|
||||
// If no private IP is available it will return the first public IP, if present.
|
||||
// If no public IP is available, it will return the first loopback IP, if present.
|
||||
func findIP(addresses []net.Addr) (net.IP, error) {
|
||||
var publicIP net.IP
|
||||
var localIP net.IP
|
||||
|
||||
for _, rawAddr := range addresses {
|
||||
var ip net.IP
|
||||
switch addr := rawAddr.(type) {
|
||||
case *net.IPAddr:
|
||||
ip = addr.IP
|
||||
case *net.IPNet:
|
||||
ip = addr.IP
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if ip.IsLoopback() {
|
||||
if localIP == nil {
|
||||
localIP = ip
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if !ip.IsPrivate() {
|
||||
if publicIP == nil {
|
||||
publicIP = ip
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Return private IP if available
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// Return public or virtual IP
|
||||
if len(publicIP) > 0 {
|
||||
return publicIP, nil
|
||||
}
|
||||
|
||||
// Return local IP
|
||||
if len(localIP) > 0 {
|
||||
return localIP, nil
|
||||
}
|
||||
|
||||
return nil, ErrIPNotFound
|
||||
}
|
||||
Reference in New Issue
Block a user