How hmgle/graftcp works
Main Components
• Graftcp (Tracer Component):
– Written in C (files such as graftcp.c, cidr-trie.c/h, conf.c/h, main.c, etc.),
– Its job is to attach to any target program (using ptrace) and intercept connect(2) syscalls.
– It modifies the syscall arguments so that the connection is redirected to the local proxy (graftcp-local).
– Communicates critical destination connection information via a pipe (or FIFO) to the proxy.
• Graftcp-local (Local Proxy Component):
– Written in Go (files under local/, including the main.go in local/cmd/graftcp-local),
– Listens on a defined local port (default is :2233) for “grafted” connections.
– Processes the original destination info sent through the pipe and then establishes a connection either to a SOCKS5 or an HTTP proxy.
– Provides a configuration interface and supports logging, service control, and integration with systemd.
• mgraftcp (Combined Binary):
– Acts as a combined executable that integrates the functionality of both graftcp and graftcp-local.
– It streamlines the user experience by eliminating the need to run the two components separately.
• Configuration and Example Files:
– There are example configuration files (like example-graftcp.conf and example-blacklist-ip.txt/whitelist-ip.txt).
– The project contains Makefiles to build both the C and Go components.
– Service files (for systemd) are provided under local/contrib/systemd to help deploy the local proxy as a service.
• External Systems:
– The actual proxy servers (SOCKS5 and/or HTTP) are external to this project.
Out Relationships and Data Flows
• Target application (“tracee”) initiating a TCP connection.
• That graftcp sends the intercepted destination address (via a pipe) to graftcp-local.
• From graftcp-local, illustrate how it speaks to the external proxy server (SOCKS5 or HTTP) establishing the actual connection.
• Optionally, indicate the implications of running mgraftcp, which merges the above steps into one binary.
Architectural Patterns and Principles
• Separation of Concerns: The design clearly partitions the syscall interception (C-based “tracer”) from the connection management and external proxy handling (Go-based “local proxy”).
• Inter-Process Communication (IPC): Emphasize the use of pipes/FIFOs for passing destination data between graftcp and graftcp-local.
• Dynamic Hooking and Redirection: The use of ptrace for intercepting and modifying system calls is a central design idea.
• Configurability: The multiple configuration files and options for controlling behavior (e.g., blacklist/whitelist IP files, proxy selection modes) should also be noted.
Relevant Technologies, Frameworks, and Services
• C: For low-level system call tracing and hooking.
• Go: For building the local proxy management daemon (graftcp-local) with networking, configuration, and logging capabilities.
• ptrace(2): As the Linux kernel interface used for tracing.
• systemd: For service management (the provided systemd service file shows integration with Linux service management).
• External Proxies (SOCKS5/HTTP): These are the targets for actual outbound connections.
• Make & Go Modules: For building dependencies and compiling the project.