All Products
Search
Document Center

API Gateway:geo-ip

Last Updated:Feb 13, 2025

The geo-ip plug-in can obtain user geological locations based on user IP addresses and then pass the locations to subsequent plug-ins as a request attribute or header.

Running attributes

Plug-in execution stage: Authorization. Plug-in execution priority: 440.

Fields

Field

Data type

Required

Default value

Description

ip_protocol

string

No

ipv4

The IP protocol to be used. Valid values: ipv4 and ipv6. If ipv4 is specified, only the geographical information of IPv4 users is collected and passed to subsequent plug-ins. Requests from IPv6 users skip the plug-in. Support for ipv6 is under development. When ipv6 is supported and specified, only the geographical information of IPv6 users is collected and passed to subsequent plug-ins. Requests from IPv4 users skip the plug-in.

ip_source_type

string

No

origin-source

The source type. Valid values: origin-source and header. origin-source indicates the peer-end socket IP address.

ip_header_name

string

No

x-forwarded-for

The header from which an IP address is obtained. This field takes effect when ip_source_type is set to header.

Configuration examples

ip_protocol: ipv4
ip_source_type: header
ip_header_name: X-Real-Ip

Generate geoCidr.txt

The ip.merge.txt file in the generateCidr directory contains all CIDR blocks around the world in the ip2region project on GitHub. ipRange2Cidr.go is a program that converts a CIDR block into multiple CIDR blocks. The converted CIDR blocks and geographical information are stored in the /data/geoCidr.txt file. The geo-ip plug-in reads the geoCidr.txt file and parses it to the memory of the radixtree data structure when Higress starts up to read configurations. This helps you query geographical information corresponding to user IP addresses. Run the following command to start ipRange2Cidr.go:

go run generateCidr/ipRange2Cidr.go

How to use properties

You can call proxywasm.SetProperty() in the geo-ip plug-in to set country, city, province, and isp as request properties. This way, the plug-in can call proxywasm.GetProperty() to obtain the geographical information corresponding to user IP addresses.

Carry user geographic locations by combining the AI prompt decorator plug-in and the geo-ip plug-in

If you want to prepend or append user geographic locations in LLM requests, you must keep both the geo-ip plug-in and the AI prompt plug-in enabled. In addition, you must ensure that the geo-ip plug-in have a higher priority than the AI prompt decorator plug-in in the same processing stage. This way, the geo-ip plug-in calculates the user's geographic location based on the user IP address, and then passes the location information to subsequent plug-ins by using a request attribute. For example, in the default phase, the priority of the geo-ip plug-in is 1000, and that of the ai-prompt-decorator plug-in is 500.

Configuration example of a geo-ip plug-in:

ipProtocal: "ipv4"

Configuration example of an ai-prompt plug-in:

prepend:
- role: system
  content: "The current geographic location of the user is: country:${geo-country}, province:${geo-province}, city:${geo-city}"
append:
- role: user
  content: "Each time you answer a question, try to ask a rhetorical question."

Use the preceding configurations to initiate a request.

curl http://localhost/test \
-H "content-type: application/json" \
-H "x-forwarded-for: 87.254.207.100,4.5.6.7" \
-d '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "user",
      "content": "What is the weather like today?"
    }
  ]
}'

The following code shows a sample processed request:

curl http://localhost/test \
-H "content-type: application/json" \
-H "x-forwarded-for: 87.254.207.100,4.5.6.7" \
-d '{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      content: "The current geographic location of the user is: country: China, province: Beijing, city: Beijing"
    },
    {
      "role": "user",
      "content": "What is the weather like today?"
    },
    {
      "role": "user",
      content: "Each time you answer a question, try to ask a rhetorical question."
    }
  ]
}'