diff --git a/irc/client.go b/irc/client.go index 017dfac0..309f3f6f 100644 --- a/irc/client.go +++ b/irc/client.go @@ -33,7 +33,7 @@ func NewClient(server *Server, conn net.Conn) *Client { client := &Client{ channels: make(ChannelSet), conn: conn, - hostname: LookupHostname(conn.RemoteAddr()), + hostname: AddrLookupHostname(conn.RemoteAddr()), replies: replies, server: server, } diff --git a/irc/net.go b/irc/net.go index f1333255..30c7ab49 100644 --- a/irc/net.go +++ b/irc/net.go @@ -63,19 +63,19 @@ func StringWriteChan(conn net.Conn) chan<- string { return ch } -func LookupHostname(addr net.Addr) string { +func AddrLookupHostname(addr net.Addr) string { addrStr := addr.String() ipaddr, _, err := net.SplitHostPort(addrStr) if err != nil { return addrStr } - switch ipaddr { - case "127.0.0.1", "::1": - return "localhost" - } - names, err := net.LookupAddr(ipaddr) - if err != nil { - return ipaddr - } - return names[0][0 : len(names[0])-1] + return LookupHostname(ipaddr) +} + +func LookupHostname(addr string) string { + names, err := net.LookupAddr(addr) + if err != nil { + return addr + } + return strings.TrimSuffix(names[0], ".") } diff --git a/irc/server.go b/irc/server.go index d56e0019..576a8008 100644 --- a/irc/server.go +++ b/irc/server.go @@ -14,7 +14,6 @@ type Server struct { channels ChannelNameMap commands chan<- Command ctime time.Time - hostname string name string operators map[string]string password string @@ -94,7 +93,6 @@ func (s *Server) listen(config ListenerConfig) { log.Fatal("Server.Listen: ", err) } - s.hostname = LookupHostname(listener.Addr()) log.Print("Server.Listen: listening on ", config.Address) for { @@ -404,10 +402,5 @@ func (msg *CapCommand) HandleServer(server *Server) { } func (msg *ProxyCommand) HandleServer(server *Server) { - client := msg.Client() - addr, err := net.ResolveIPAddr("ip", msg.sourceIP) - if err != nil { - return - } - client.hostname = LookupHostname(addr) + msg.Client().hostname = LookupHostname(msg.sourceIP) }