diff options
| author | jackyzha0 <[email protected]> | 2021-03-07 08:13:10 -0800 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2021-03-07 08:13:10 -0800 |
| commit | a9b72af75a9f9cc2be8cdec581133c0e9a301520 (patch) | |
| tree | f8d966ca52919047eb8fa74822af7c5d90d29710 /backend | |
| parent | Merge pull request #71 from jackyzha0/http-refactor (diff) | |
| download | ctrl-v-a9b72af75a9f9cc2be8cdec581133c0e9a301520.tar.xz ctrl-v-a9b72af75a9f9cc2be8cdec581133c0e9a301520.zip | |
remove ip from hash calculation
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/api/ip.go | 37 | ||||
| -rw-r--r-- | backend/api/routes.go | 5 | ||||
| -rw-r--r-- | backend/db/db.go | 4 | ||||
| -rw-r--r-- | backend/security/hash.go | 4 |
4 files changed, 5 insertions, 45 deletions
diff --git a/backend/api/ip.go b/backend/api/ip.go deleted file mode 100644 index 0d135b3..0000000 --- a/backend/api/ip.go +++ /dev/null @@ -1,37 +0,0 @@ -package api - -import ( - "net" - "net/http" - "strings" -) - -func getIP(r *http.Request) (s string) { - // Get IP from the X-REAL-IP header - ip := r.Header.Get("X-REAL-IP") - netIP := net.ParseIP(ip) - if netIP != nil { - return ip - } - - // Get IP from X-FORWARDED-FOR header - ips := r.Header.Get("X-FORWARDED-FOR") - splitIps := strings.Split(ips, ",") - for _, ip := range splitIps { - netIP := net.ParseIP(ip) - if netIP != nil { - return ip - } - } - - // Get IP from RemoteAddr - ip, _, err := net.SplitHostPort(r.RemoteAddr) - if err != nil { - return - } - netIP = net.ParseIP(ip) - if netIP != nil { - return - } - return -} diff --git a/backend/api/routes.go b/backend/api/routes.go index d05e99a..c4a457f 100644 --- a/backend/api/routes.go +++ b/backend/api/routes.go @@ -30,11 +30,8 @@ func insertFunc(w http.ResponseWriter, r *http.Request) { password := r.FormValue("password") lang := r.FormValue("language") - // get ip - ip := getIP(r) - // insert content - hash, err := db.New(ip, content, expiry, title, password, lang) + hash, err := db.New(content, expiry, title, password, lang) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "%s", err.Error()) diff --git a/backend/db/db.go b/backend/db/db.go index af68262..a9631af 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -29,9 +29,9 @@ const TitleLimit = 100 const ContentLimit = 100000 // creates a new paste with title, content and hash, returns the hash of the created paste -func New(ip, content, expiry, title, password, lang string) (string, error) { +func New(content, expiry, title, password, lang string) (string, error) { // generate hash from ip - hash := security.GenerateURI(ip) + hash := security.GenerateURI(content) // check for size of title and content errs := checkLengths(title, content) diff --git a/backend/security/hash.go b/backend/security/hash.go index b6ce167..cf7ef61 100644 --- a/backend/security/hash.go +++ b/backend/security/hash.go @@ -11,9 +11,9 @@ import ( const UrlLength = 7 // GenerateURI creates a unique identifier for a paste based on ip and timestamp -func GenerateURI(ip string) string { +func GenerateURI(content string) string { timeStamp := time.Now().String() - return hashString(ip + timeStamp)[:UrlLength] + return hashString(content + timeStamp)[:UrlLength] } // hashes using MD5 and then converts to base 62 |