optimize ip address location logic

pull/136/head
alimy 2 years ago
parent 115a66dfca
commit 6bc7f5e636

@ -11,7 +11,6 @@ import (
var (
_ core.DataService = (*dataServant)(nil)
_ core.AttachmentCheckService = (*attachmentCheckServant)(nil)
_ core.TweetSearchService = (*zincTweetSearchServant)(nil)
)
type dataServant struct {

@ -104,10 +104,6 @@ func tagsFrom(originTags []string) []string {
// TODO: maybe have bug need optimize for use transaction to create post
func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Post, error) {
ip := c.ClientIP()
if len(ip) == 0 {
ip = "未知"
}
tags := tagsFrom(param.Tags)
post := &model.Post{
UserID: userID,

@ -1 +0,0 @@
package types

@ -20,20 +20,27 @@ const (
cRedirectMode2 = 0x02
)
// Find get country and city base ip
// Find get country and city base ip. return empty country
// and city when ip that pass from argument is empty string
// or invalid ip address that net.ParseIP(...).To4 return nil
// eg: "::1"
func Find(ip string) (string, string) {
if strings.Trim(ip, " ") == "" {
ip = strings.Trim(ip, " ")
if len(ip) == 0 {
return "", ""
}
// If ip is "::1", To4 returns nil.
to4 := net.ParseIP(ip).To4()
// If ip is "::1", To4 returns nil.
if to4 == nil {
to4 = net.ParseIP("127.0.0.1").To4()
return "", ""
}
offset := searchIndex(binary.BigEndian.Uint32(to4))
if offset <= 0 {
return "", ""
}
var country, area []byte
mode := readMode(offset + 4)
if mode == cRedirectMode1 {

Loading…
Cancel
Save