Skip to content

Commit

Permalink
feat: ✨ Optional ip geolocation, a Test
Browse files Browse the repository at this point in the history
  • Loading branch information
onionj committed Jan 17, 2024
1 parent 4a489c0 commit ae6edd4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Define the output directory for the builds
OUTPUT_DIR := ./build
version := 1.6.4
version := 1.7.0

# Define the name of your binary
BINARY_NAME := yourip
Expand Down
24 changes: 5 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,18 @@ You can access the YourIP Server at [yourip.top](http://yourip.top).

## Run Server:

### Method One:


```bash
make
yourip -b 80 -g
```
```bash
./yourip 'host:port'
```

Replace `'host:port'` with the desired host and port to listen on.

For example:

```bash
./yourip :8080
```

### Method Two:

```bash
go run main.go 'host:port'
--bind -b
The address to bind the TCP server to. (default ":8080")
--geolocation -g
Optional IP geolocation service. (its downlaod Country IP blocks database (70MB) )
```

Replace `'host:port'` with the desired host and port to listen on.

## How it works

The server uses the `net` package in Go to resolve the TCP address, start a listener, and accept incoming connections. It sets a deadline of one minute for each connection and responds with an HTTP 200 OK message containing the client's IP address.
Expand Down
28 changes: 28 additions & 0 deletions geolocation/geolocation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package geolocation

import (
"net"
"testing"
)

func TestQueryIPV4(t *testing.T) {

want := "us"
ipRangeMap := make(map[string][]*net.IPNet)
ipRange := "1.1.1.1/24"
_, ipNet, _ := net.ParseCIDR(ipRange)

ipRangeMap[want] = append(ipRangeMap[want], ipNet)

ipGeoloaction := IPGeolocation{
CIDRListV4: ipRangeMap,
Ready: true,
}

testCase := net.IPv4(byte(1), byte(1), byte(1), byte(1))

got, err := ipGeoloaction.Query(testCase)
if got != want {
t.Errorf("got %q, wanted %q, error: %q", got, want, err.Error())
}
}
46 changes: 30 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"net"
Expand Down Expand Up @@ -44,19 +45,25 @@ type JsonResponse struct {
Country string `json:"country"`
}

var ipGeolocation *geolocation.IPGeolocation
var (
bindAddress string
useIPGeolocation bool
ipGeolocation *geolocation.IPGeolocation
)

func main() {
flag.StringVar(&bindAddress, "bind", ":8080", "The address to bind the TCP server to. (shorthand -b)")
flag.StringVar(&bindAddress, "b", ":8080", "(shorthand for --bind)")

if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "'host:port'")
os.Exit(1)
}
service := os.Args[1]
flag.BoolVar(&useIPGeolocation, "geolocation", false, "ip geolocation service (shorthand -g)")
flag.BoolVar(&useIPGeolocation, "g", false, "(shorthand for --geolocation)")
flag.Parse()

ipGeolocation = geolocation.New(time.Duration(48))
if useIPGeolocation {
ipGeolocation = geolocation.New(time.Duration(48))
}

tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
tcpAddr, err := net.ResolveTCPAddr("tcp4", bindAddress)
checkError(err)

listener, err := net.ListenTCP("tcp", tcpAddr)
Expand Down Expand Up @@ -139,12 +146,19 @@ func checkError(err error) {
// http JSON: for API Call
func CreateResponse(conn net.Conn, mode ResponseMode) (string, error) {
remoteAddr := conn.RemoteAddr().(*net.TCPAddr)
country, _ := ipGeolocation.Query(remoteAddr.IP)

country := ""
separator := ""

if useIPGeolocation {
country, _ = ipGeolocation.Query(remoteAddr.IP)
separator = " "
}

switch mode {

case ResponseModeHTTPText:
response := remoteAddr.IP.String() + " " + country
response := remoteAddr.IP.String() + separator + country
return fmt.Sprintf(RESPONSE, len(response), "text/plain", response), nil

case ResponseModeHTTPJson:
Expand All @@ -156,7 +170,7 @@ func CreateResponse(conn net.Conn, mode ResponseMode) (string, error) {
return fmt.Sprintf(RESPONSE, len(jsonResponseByte), "application/json", jsonResponseByte), nil

default:
return remoteAddr.IP.String() + " " + country, nil
return remoteAddr.IP.String() + separator + country, nil
}
}

Expand Down Expand Up @@ -188,11 +202,11 @@ func StreamAnimation(conn net.Conn, response string, animationMode string) {
fmt.Printf("(%s) Stream animation to %s (Flight)\n", time.Now().String()[:23], conn.RemoteAddr().String())

response = " " + response + " "
flight := ` %s\
%s| |~\______/~~\__ |
%s|______ \_____======= )-+
%s| |/ |
%s/ () `
flight := ` %s\
%s| |~\______/~~\__ |
%s|______ \_____======= )-+
%s| |/ |
%s/ () `
flight = fmt.Sprintf(
flight,
strings.Repeat("-", len(response)),
Expand Down

0 comments on commit ae6edd4

Please sign in to comment.