Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ You can also specify the following URI to retrieve certain info:
- `asn`: AS number
- `as_desc`: AS description
- `user_agent`: User agent string
- `lookup`: Query information for a specific IP address (or client IP if not provided)

Examples:

Expand Down Expand Up @@ -111,6 +112,11 @@ Google LLC

$ wget -qO- https://ipinfo.tw/user_agent
Wget

$ curl https://ipinfo.tw/lookup?ip=1.1.1.1
Copy link

Copilot AI May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider clarifying the example output by explaining that the output lines represent the queried IP address, the country (with region if applicable), and the ASN details, which could help users better understand the endpoint response.

Copilot uses AI. Check for mistakes.
1.1.1.1
AU / Australia
AS13335 / CLOUDFLARENET
```

##### Database build time endpoint
Expand Down
10 changes: 9 additions & 1 deletion nginx/conf.d/geoip2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$ip_country_code source=$remote_addr country iso_code;
$ip_country_name source=$remote_addr country names en;
$ip_country_build_epoch metadata build_epoch;

$lookup_country_code source=$lookup_ip country iso_code;
$lookup_country_name source=$lookup_ip country names en;
$lookup_country_build_epoch metadata build_epoch;
}

geoip2 /usr/share/GeoIP/GeoLite2-ASN.mmdb {
auto_reload 1d;
$ip_asn source=$remote_addr autonomous_system_number;
$ip_aso source=$remote_addr autonomous_system_organization;
$ip_as_build_epoch metadata build_epoch;
}

$lookup_asn source=$lookup_ip autonomous_system_number;
$lookup_aso source=$lookup_ip autonomous_system_organization;
$lookup_as_build_epoch metadata build_epoch;
}
6 changes: 6 additions & 0 deletions nginx/conf.d/ipinfo.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ server {

set $ip_country "$ip_country_code / $ip_country_name";
set $ip_as "AS$ip_asn / $ip_aso";
set $lookup_country "$lookup_country_code / $lookup_country_name";
set $lookup_as "AS$lookup_asn / $lookup_aso";

location = /favicon.ico {
log_not_found off;
Expand Down Expand Up @@ -64,6 +66,10 @@ server {
default_type application/json;
return 200 "{\"GeoLite2-Country\":\"$ip_country_build_epoch\",\"GeoLite2-ASN\":\"$ip_as_build_epoch\"}\n";
}
location = /lookup {
default_type text/plain;
return 200 "$lookup_ip\n$lookup_country\n$lookup_as\n\n$http_user_agent\n";
}

location @404 {
return 404 "404 not found. See https://github.com/PeterDaveHello/ipinfo.tw \n";
Expand Down
4 changes: 4 additions & 0 deletions nginx/conf.d/map.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
map $arg_ip $lookup_ip {
"" $remote_addr;
default $arg_ip;
}
1 change: 1 addition & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ http {
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
map_hash_bucket_size 128;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I referred to the explanation in the issue #20 , but I'm not sure if it's really necessary.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai can you help @che0124?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check the content of issue #20 to see what it says about map_hash_bucket_size
gh issue view 20 -R PeterDaveHello/ipinfo.tw

# Check the content of the map.conf file to understand the mapping
cat nginx/conf.d/map.conf

# Also look at the implementation to understand how complex the mapping is
cat nginx/conf.d/geoip2.conf
cat nginx/conf.d/ipinfo.conf

Length of output: 4382


@PeterDaveHello @che0124 Based on my analysis, increasing map_hash_bucket_size to 128 does not appear necessary for this implementation.

The map_hash_bucket_size directive controls the memory allocation for hash tables used by the map directive. The default value (32 or 64 depending on platform) should be sufficient for the simple map in map.conf:

map $arg_ip $lookup_ip {
    "" $remote_addr;
    default $arg_ip;
}

This directive typically only needs to be increased when:

  1. You have maps with very long string values
  2. You encounter the error "could not build the map_hash, you should increase map_hash_bucket_size"
  3. You have a large number of entries in your map

Since the current map is very simple with only two short entries, the default value should be more than adequate. I recommend removing this line unless you've encountered specific issues during testing that required this increase.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ask explain the map_hash_bucket_size 128; usage here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map_hash_bucket_size 128; directive in Nginx configuration sets the size of the hash table buckets used for the map directive.

This setting is important when:

  • You have a large number of mappings in map blocks
  • You're using complex or large-scale IP-to-value or string-to-value mappings
  • You're getting "hash bucket size" errors in Nginx logs

By increasing the bucket size to 128 (from the default which is typically 32 or 64), the PR is ensuring that the hash tables used for mapping operations have enough memory allocated to handle a larger number of entries efficiently.

This change is likely related to the "Add IP lookup feature" mentioned in the PR title, as IP lookups often require mapping IP addresses to specific values or behaviors, which would use Nginx's map directive.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@che0124 do you mean #21 ? And the size was also revised?


include /etc/nginx/conf.d/*.conf;
}