Function Description

The geo-ip plugin allows querying geographical location information based on the user’s IP address, and then passes this geographical information to subsequent plugins through request attributes and newly added request headers.

Runtime Properties

Plugin Execution Phase: Authentication Phase
Plugin Execution Priority: 440

Configuration Fields

NameData TypeRequirementDefault ValueDescription
ip_protocolstringNoipv4Optional values: 1. ipv4: Only queries geographical location information for ipv4 user requests, passing it to subsequent plugins. Requests from ipv6 users will skip this plugin and be processed by later plugins. 2. ipv6: (To be implemented in the future) Only queries geographical location information for ipv6 users, passing it to subsequent plugins. Requests from ipv4 users will skip this plugin and be processed by later plugins. (Currently skips the plugin; requests are handled by subsequent plugins.)
ip_source_typestringNoorigin-sourceOptional values: 1. Peer socket IP: origin-source; 2. Retrieved via header: header
ip_header_namestringNox-forwarded-forWhen ip_source_type is header, specify the custom IP source header.

Configuration Example

  1. ip_protocol: ipv4
  2. ip_source_type: header
  3. ip_header_name: X-Real-Ip

Explanation for Generating geoCidr.txt

The ip.merge.txt file included in the generateCidr directory is the global IP segment database from the ip2region project on GitHub. The ipRange2Cidr.go program converts IP segments into multiple CIDRs. The converted CIDRs and geographical location information are stored in the /data/geoCidr.txt file. The geo-ip plugin will read the geoCidr.txt file during the configuration stage when Higress starts and parse it into the radixtree data structure in memory for future queries of geographical location information corresponding to user IP addresses. The command to run the conversion program is as follows:

  1. go run generateCidr/ipRange2Cidr.go

Usage of Properties

In the geo-ip plugin, call proxywasm.SetProperty() to set country, city, province, and isp into request attributes so that subsequent plugins can use proxywasm.GetProperty() to obtain the geographical information corresponding to the user’s IP for that request.

Expanding the AI Prompt Decorator Plugin with User Geolocation Information Based on Geo-IP Plugin Capabilities

If you need to include user geolocation information before and after LLM requests, make sure both the geo-ip plugin and the AI prompt decorator plugin are enabled. Additionally, in the same request processing phase, the priority of the geo-ip plugin must be higher than that of the AI prompt decorator plugin. First, the geo-ip plugin will calculate the user’s geolocation based on their IP address and then pass this information to subsequent plugins via request attributes. For example, in the default phase, the priority of the geo-ip plugin is set to 1000, while the priority of the ai-prompt-decorator plugin is set to 500.

Example configuration for the geo-ip plugin:

  1. ipProtocol: ipv4

Example configuration for the AI prompt decorator plugin:

  1. prepend:
  2. - role: system
  3. content: The current location of the user asking the question is, country: ${geo-country}, province: ${geo-province}, city: ${geo-city}”
  4. append:
  5. - role: user
  6. content: After answering each question, try to ask a follow-up question

Using the above configuration to send a request:

  1. -H content-type: application/json \
  2. -H x-forwarded-for: 87.254.207.100,4.5.6.7 \
  3. -d ‘{
  4. model”: gpt-3.5-turbo”,
  5. messages”: [
  6. {
  7. role”: user”,
  8. content”: Whats the weather like today?”
  9. }
  10. ]
  11. }’

After processing by the plugins, the actual request becomes:

  1. -H content-type: application/json \
  2. -H x-forwarded-for: 87.254.207.100,4.5.6.7 \
  3. -d ‘{
  4. model”: gpt-3.5-turbo”,
  5. messages”: [
  6. {
  7. role”: system”,
  8. content”: The current location of the user asking the question is, country: China, province: Beijing, city: Beijing
  9. },
  10. {
  11. role”: user”,
  12. content”: Whats the weather like today?”
  13. },
  14. {
  15. role”: user”,
  16. content”: After answering each question, try to ask a follow-up question
  17. }
  18. ]
  19. }’