mirror of
https://github.com/ergochat/ergo.git
synced 2024-11-15 00:19:29 +01:00
factor out some shared code
This commit is contained in:
parent
657ce0f1a4
commit
e0e4791f72
@ -488,40 +488,19 @@ func (client *Client) lookupHostname(session *Session, overwrite bool) {
|
|||||||
if session.proxiedIP != nil {
|
if session.proxiedIP != nil {
|
||||||
ip = session.proxiedIP
|
ip = session.proxiedIP
|
||||||
}
|
}
|
||||||
ipString := ip.String()
|
|
||||||
|
|
||||||
var hostname, candidate string
|
var hostname string
|
||||||
|
lookupSuccessful := false
|
||||||
if config.Server.lookupHostnames {
|
if config.Server.lookupHostnames {
|
||||||
session.Notice("*** Looking up your hostname...")
|
session.Notice("*** Looking up your hostname...")
|
||||||
|
hostname, lookupSuccessful = utils.LookupHostname(ip, config.Server.ForwardConfirmHostnames)
|
||||||
names, err := net.LookupAddr(ipString)
|
if lookupSuccessful {
|
||||||
if err == nil && 0 < len(names) {
|
session.Notice("*** Found your hostname")
|
||||||
candidate = strings.TrimSuffix(names[0], ".")
|
} else {
|
||||||
}
|
|
||||||
if utils.IsHostname(candidate) {
|
|
||||||
if config.Server.ForwardConfirmHostnames {
|
|
||||||
addrs, err := net.LookupHost(candidate)
|
|
||||||
if err == nil {
|
|
||||||
for _, addr := range addrs {
|
|
||||||
if addr == ipString {
|
|
||||||
hostname = candidate // successful forward confirmation
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
hostname = candidate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if hostname != "" {
|
|
||||||
session.Notice("*** Found your hostname")
|
|
||||||
} else {
|
|
||||||
if config.Server.lookupHostnames {
|
|
||||||
session.Notice("*** Couldn't look up your hostname")
|
session.Notice("*** Couldn't look up your hostname")
|
||||||
}
|
}
|
||||||
hostname = utils.IPStringToHostname(ipString)
|
} else {
|
||||||
|
hostname = utils.IPStringToHostname(ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
session.rawHostname = hostname
|
session.rawHostname = hostname
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime/debug"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -156,12 +155,7 @@ func histservExportHandler(service *ircService, server *Server, client *Client,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func histservExportAndNotify(service *ircService, server *Server, cfAccount string, outfile *os.File, filename, alertNick string) {
|
func histservExportAndNotify(service *ircService, server *Server, cfAccount string, outfile *os.File, filename, alertNick string) {
|
||||||
defer func() {
|
defer server.HandlePanic()
|
||||||
if r := recover(); r != nil {
|
|
||||||
server.logger.Error("history",
|
|
||||||
fmt.Sprintf("Panic in history export routine: %v\n%s", r, debug.Stack()))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
defer outfile.Close()
|
defer outfile.Close()
|
||||||
writer := bufio.NewWriter(outfile)
|
writer := bufio.NewWriter(outfile)
|
||||||
|
19
irc/panic.go
Normal file
19
irc/panic.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright (c) 2021 Shivaram Lingamneni
|
||||||
|
// released under the MIT license
|
||||||
|
|
||||||
|
package irc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandlePanic is a general-purpose panic handler for ad-hoc goroutines.
|
||||||
|
// Because of the semantics of `recover`, it must be called directly
|
||||||
|
// from the routine on whose call stack the panic would occur, with `defer`,
|
||||||
|
// e.g. `defer server.HandlePanic()`
|
||||||
|
func (server *Server) HandlePanic() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
server.logger.Error("internal", fmt.Sprintf("Panic encountered: %v\n%s", r, debug.Stack()))
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,6 @@ import (
|
|||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime/debug"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -245,14 +244,12 @@ func (server *Server) checkTorLimits() (banned bool, message string) {
|
|||||||
|
|
||||||
func (server *Server) handleAlwaysOnExpirations() {
|
func (server *Server) handleAlwaysOnExpirations() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
// reschedule whether or not there was a panic
|
||||||
server.logger.Error("internal",
|
|
||||||
fmt.Sprintf("Panic in always-on cleanup: %v\n%s", r, debug.Stack()))
|
|
||||||
}
|
|
||||||
// either way, reschedule
|
|
||||||
time.AfterFunc(alwaysOnExpirationPollPeriod, server.handleAlwaysOnExpirations)
|
time.AfterFunc(alwaysOnExpirationPollPeriod, server.handleAlwaysOnExpirations)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
defer server.HandlePanic()
|
||||||
|
|
||||||
config := server.Config()
|
config := server.Config()
|
||||||
deadline := time.Duration(config.Accounts.Multiclient.AlwaysOnExpiration)
|
deadline := time.Duration(config.Accounts.Multiclient.AlwaysOnExpiration)
|
||||||
if deadline == 0 {
|
if deadline == 0 {
|
||||||
@ -514,16 +511,7 @@ func (client *Client) getWhoisOf(target *Client, hasPrivs bool, rb *ResponseBuff
|
|||||||
// rehash reloads the config and applies the changes from the config file.
|
// rehash reloads the config and applies the changes from the config file.
|
||||||
func (server *Server) rehash() error {
|
func (server *Server) rehash() error {
|
||||||
// #1570; this needs its own panic handling because it can be invoked via SIGHUP
|
// #1570; this needs its own panic handling because it can be invoked via SIGHUP
|
||||||
defer func() {
|
defer server.HandlePanic()
|
||||||
if r := recover(); r != nil {
|
|
||||||
if server.Config().Debug.recoverFromErrors {
|
|
||||||
server.logger.Error("internal",
|
|
||||||
fmt.Sprintf("Panic during rehash: %v\n%s", r, debug.Stack()))
|
|
||||||
} else {
|
|
||||||
panic(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
server.logger.Info("server", "Attempting rehash")
|
server.logger.Info("server", "Attempting rehash")
|
||||||
|
|
||||||
|
@ -193,3 +193,37 @@ func HandleXForwardedFor(remoteAddr string, xForwardedFor string, whitelist []ne
|
|||||||
// or nil:
|
// or nil:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LookupHostname does an (optionally reverse-confirmed) hostname lookup
|
||||||
|
// suitable for use as an IRC hostname. It falls back to a string
|
||||||
|
// representation of the IP address (again suitable for use as an IRC
|
||||||
|
// hostname).
|
||||||
|
func LookupHostname(ip net.IP, forwardConfirm bool) (hostname string, lookupSuccessful bool) {
|
||||||
|
ipString := ip.String()
|
||||||
|
var candidate string
|
||||||
|
names, err := net.LookupAddr(ipString)
|
||||||
|
if err == nil && 0 < len(names) {
|
||||||
|
candidate = strings.TrimSuffix(names[0], ".")
|
||||||
|
}
|
||||||
|
if IsHostname(candidate) {
|
||||||
|
if forwardConfirm {
|
||||||
|
addrs, err := net.LookupHost(candidate)
|
||||||
|
if err == nil {
|
||||||
|
for _, addr := range addrs {
|
||||||
|
if forwardIP := net.ParseIP(addr); ip.Equal(forwardIP) {
|
||||||
|
hostname = candidate // successful forward confirmation
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
hostname = candidate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hostname != "" {
|
||||||
|
return hostname, true
|
||||||
|
} else {
|
||||||
|
return IPStringToHostname(ipString), false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user