mirror of
https://gogs.blitter.com/RLabs/goutmp
synced 2024-08-14 19:26:41 +00:00
GetHost() now handles IPv4 and IPv6 addresses
This commit is contained in:
parent
448de8b524
commit
ab96663864
2 changed files with 29 additions and 22 deletions
48
goutmp.go
48
goutmp.go
|
@ -78,6 +78,7 @@ import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os/user"
|
"os/user"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -86,46 +87,51 @@ import (
|
||||||
|
|
||||||
// UtmpEntry wraps the C struct utmp
|
// UtmpEntry wraps the C struct utmp
|
||||||
type UtmpEntry struct {
|
type UtmpEntry struct {
|
||||||
entry C.struct_utmp
|
entry C.struct_utmp
|
||||||
}
|
}
|
||||||
|
|
||||||
// return remote client hostname or IP if host lookup fails
|
// return remote client hostname or IP if host lookup fails
|
||||||
|
// addr is expected to be of the format given by net.Addr.String()
|
||||||
|
// eg., "127.0.0.1:80" or "[::1]:80"
|
||||||
func GetHost(addr string) (h string) {
|
func GetHost(addr string) (h string) {
|
||||||
// Strip off the port after IP addr
|
if !strings.Contains(addr, "[") {
|
||||||
hList, e := net.LookupAddr(strings.Split(addr,":")[0])
|
h = strings.Split(addr, ":")[0]
|
||||||
fmt.Printf("lookupAddr:%v\n", hList)
|
|
||||||
if e != nil {
|
|
||||||
h = strings.Split(addr,":")[0]
|
|
||||||
} else {
|
} else {
|
||||||
|
h = strings.Split(strings.Split(addr, "[")[1], "]")[0]
|
||||||
|
}
|
||||||
|
hList, e := net.LookupAddr(h)
|
||||||
|
//fmt.Printf("lookupAddr:%v\n", hList)
|
||||||
|
if e == nil {
|
||||||
h = hList[0]
|
h = hList[0]
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put a username and the originating host/IP to utmp
|
// Put a username and the originating host/IP to utmp
|
||||||
func Put_utmp(user string, host string) (UtmpEntry) {
|
func Put_utmp(user string, host string) UtmpEntry {
|
||||||
var entry UtmpEntry
|
var entry UtmpEntry
|
||||||
|
|
||||||
C.pututmp(&entry.entry, C.CString(user), C.CString(host))
|
log.Println("Put_utmp:host ", host, " user ", user)
|
||||||
return entry
|
C.pututmp(&entry.entry, C.CString(user), C.CString(host))
|
||||||
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove a username/host entry from utmp
|
// Remove a username/host entry from utmp
|
||||||
func Unput_utmp(entry UtmpEntry) {
|
func Unput_utmp(entry UtmpEntry) {
|
||||||
C.unpututmp(&entry.entry)
|
C.unpututmp(&entry.entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the login app, username and originating host/IP to lastlog
|
// Put the login app, username and originating host/IP to lastlog
|
||||||
func Put_lastlog_entry(app string, usr string, host string) {
|
func Put_lastlog_entry(app string, usr string, host string) {
|
||||||
u, e := user.Lookup(usr)
|
u, e := user.Lookup(usr)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var uid uint32
|
var uid uint32
|
||||||
fmt.Sscanf(u.Uid, "%d", &uid)
|
fmt.Sscanf(u.Uid, "%d", &uid)
|
||||||
|
|
||||||
t := time.Now().Unix()
|
t := time.Now().Unix()
|
||||||
_ = C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
|
_ = C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
|
||||||
//stat := C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
|
//stat := C.putlastlogentry(C.int64_t(t), C.int(uid), C.CString(app), C.CString(host))
|
||||||
//fmt.Println("stat was:",stat)
|
//fmt.Println("stat was:",stat)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"blitter.com/go/goutmp"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"blitter.com/go/goutmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue