12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package utils
- import (
- "fmt"
- "net"
- "net/http"
- "regexp"
- "sfbase/utils"
- "strings"
- )
- var (
- userAppIDEncrypt = &utils.SimpleEncrypt{"sfis20212120"}
- strReg = regexp.MustCompile("^[0-9a-zA-Z]+$")
- )
- func GetAppID(tn int64) (appID string) {
- for {
- randomstr := utils.GetLetterRandom(5)
- str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:])
- appID = userAppIDEncrypt.EncodeString(str)
- if strReg.MatchString(appID) {
- break
- }
- }
- appID = "sf" + appID
- return
- }
- func GetIp(req *http.Request) string {
- if req == nil {
- return ""
- }
- ip_for := req.Header.Get("x-forwarded-for")
- ip_client := req.Header.Get("http_client_ip")
- ip_addr := req.Header.Get("Remote_addr")
- un := "unknown"
- if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) {
- return ip_for
- }
- if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) {
- return ip_client
- }
- if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) {
- return ip_addr
- }
- ip, _, _ := net.SplitHostPort(req.RemoteAddr)
- return ip
- }
|