2 Star 0 Fork 0

slh92 / plugin-sip

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sip.go 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
slh92 提交于 2024-04-07 18:21 . 初始化代码
package utils
import (
"errors"
"fmt"
"math/rand"
"net"
"runtime"
"time"
)
func RandNum16String(n int) string {
numbers16 := "0123456789abcdef"
return randStringBySoure(numbers16, n)
}
func RandNumString(n int) string {
numbers := "0123456789"
return randStringBySoure(numbers, n)
}
func RandString(n int) string {
letterBytes := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return randStringBySoure(letterBytes, n)
}
// https://github.com/kpbird/golang_random_string
func randStringBySoure(src string, n int) string {
randomness := make([]byte, n)
rand.Seed(time.Now().UnixNano())
_, err := rand.Read(randomness)
if err != nil {
panic(err)
}
l := len(src)
// fill output
output := make([]byte, n)
for pos := range output {
random := randomness[pos]
randomPos := random % uint8(l)
output[pos] = src[randomPos]
}
return string(output)
}
// Error Error
type Error struct {
err error
params []interface{}
}
func (err *Error) Error() string {
if err == nil {
return "<nil>"
}
str := fmt.Sprint(err.params...)
if err.err != nil {
str += fmt.Sprintf(" err:%s", err.err.Error())
}
return str
}
// NewError NewError
func NewError(err error, params ...interface{}) error {
return &Error{err, params}
}
func PrintStack() {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
fmt.Printf("==> %s\n", string(buf[:n]))
}
// ResolveSelfIP ResolveSelfIP
func ResolveSelfIP() (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
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 || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip, nil
}
}
return nil, errors.New("server not connected to any network")
}
Go
1
https://gitee.com/slh1992/plugin-sip.git
git@gitee.com:slh1992/plugin-sip.git
slh1992
plugin-sip
plugin-sip
v1.3.9

搜索帮助

53164aa7 5694891 3bd8fe86 5694891