Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add ai-geoip wasm plugin to search the client's geographic information depending on the client ip address #1172

Merged
merged 41 commits into from
Sep 1, 2024

Conversation

ranxuxin001
Copy link
Contributor

@ranxuxin001 ranxuxin001 commented Aug 2, 2024

1. Describe what this PR did

This PR describes the implementation principle and functionality of the ai-geoip wasm plugin. When Higress starts and parses the configuration of ai-geoip plugin, it transfers rows of the ip segment and related world geographic data into a two-dimension array. It uses the first two bytes of the ip address as the two indexes of the array to get the element. After that, it appends the geoip data to an one-dimension array of the element. This one-dimension array contains several ip segments in ascendant order because the geoip rows are originally in ascendant order.

When a client sends a question to Higress, the ai-geoip plugin retrives the client ip address using the X-Forwarded-For request header or the connection remote address. And then it indexes the element of the two-dimension array according to the first two bytes of the client ip. Finally, it uses binary search to find the ip segment which includes the client ip in the one-dimension array of the element.

After that, it gets geographic data in the data structure of the ip segment. Then it delivers the geoip data structure to latter wasm plugins. using proxywasm.SetProperty(). And the latter plugin calls proxywasm.GetProperty() to take client's geoip data structure.

This plugin is able to supply the client's geographic data as the system prompt to the ai-proxy plugin which sends request to the LLVM chat api server. It helps Higress to response more accurate and localize answer to the client.

fixes #1158

2. Describe how to verify it

This plugin is verified with Higress in SAE of Alibaba cloud. Write proxywasm.GetProperty() in the latter plugin. The postman sends a request to Higress with X-Forwarded-For header. The latter plugin's log indicates the geographic data structure of the client.

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

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

这个实现的数据结构对空间浪费比较大,需要用 radix tree 实现,具体可以参考: https://github.com/zmap/go-iptree
你的原始ip data数据需要先做处理,把ip范围转换为ip段,然后通过radix tree做存储,这样可以节省大量的内存。
另外你不要把原始data数据放到go文件里,应该用embedfs等工具编译进去,并在go代码里读取文件内容。

@ranxuxin001
Copy link
Contributor Author

ranxuxin001 commented Aug 6, 2024

这个实现的数据结构对空间浪费比较大,需要用 radix tree 实现,具体可以参考: https://github.com/zmap/go-iptree 你的原始ip data数据需要先做处理,把ip范围转换为ip段,然后通过radix tree做存储,这样可以节省大量的内存。 另外你不要把原始data数据放到go文件里,应该用embedfs等工具编译进去,并在go代码里读取文件内容。

@johnlanni 非常感谢对我的实现提出宝贵意见。新的commit里是重新实现的ai-geoip-radixtree插件。我首先把ip网段转换成了cidr,结合地理位置信息一起放入 txt文件。然后用embed 把txt文件嵌入程序里。在插件里解析该文件的字符串,把cidr 做为key,地理位置信息做value,加入radixtree里。查询时候调用iptree库的GetByString(client_ip)函数获取value,得到地理位置信息。最后把用户的地理信息通过请求的 proxywasm.SetProperty() 传给后续插件。

@codecov-commenter
Copy link

codecov-commenter commented Aug 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 44.16%. Comparing base (ef31e09) to head (bf470b5).
Report is 73 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1172      +/-   ##
==========================================
+ Coverage   35.91%   44.16%   +8.24%     
==========================================
  Files          69       75       +6     
  Lines       11576     9821    -1755     
==========================================
+ Hits         4157     4337     +180     
+ Misses       7104     5156    -1948     
- Partials      315      328      +13     

see 90 files with indirect coverage changes

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

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

LGTM

plugins/wasm-go/extensions/geo-ip/main.go Show resolved Hide resolved
plugins/wasm-go/extensions/geo-ip/main.go Show resolved Hide resolved
plugins/wasm-go/extensions/geo-ip/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/geo-ip/main.go Outdated Show resolved Hide resolved
@CH3CHO CH3CHO merged commit f26cde3 into alibaba:main Sep 1, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Higress的AI插件GeoIP插件设计
4 participants