Skip to main content

SmartFoxServer 2X binary protocol implementation for Python

Project description

sfs2x-py

Pure Python implementation of the SmartFoxServer 2X binary protocol.

Features

  • Full type system: All 19 SFS2X data types (NULL, BOOL, BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, UTF_STRING, arrays, SFS_ARRAY, SFS_OBJECT)
  • C2S/S2C packet framing: Encode and decode client-to-server and server-to-client packets
  • XOR obfuscation: Rotating 4-byte XOR key derived from packet size
  • Compression: zlib and zstd support for large packets
  • AES session encryption: Key exchange via /BlueBox/CryptoManager
  • Type-preserving decode: decode_typed() wraps values in TypedValue to preserve wire types (BYTE vs INT) for MITM proxies
  • Packet builders: make_extension_request(), make_keepalive(), parse_s2c_command()

Install

pip install sfs2x-py

Or from source:

git clone https://github.com/KeepALifeUS/sfs2x-py.git
cd sfs2x-py
pip install -e ".[dev]"

Quick Start

Encode/decode SFSObject

from sfs2x import SFSCodec, TypedValue, INT

# Encode
obj = {"username": "player1", "level": TypedValue(INT, 42)}
data = SFSCodec.encode(obj)

# Decode
decoded, consumed = SFSCodec.decode(data)
print(decoded)  # {'username': 'player1', 'level': 42}

Build an extension request

from sfs2x import make_extension_request, decode_c2s_packet, TypedValue, INT

# Create a C2S packet
packet = make_extension_request(
    cmd="chat.send",
    params={"msg": "Hello!", "channel": TypedValue(INT, 1)},
    server_id=1234,
)

# Decode it back
obj, _ = decode_c2s_packet(packet)

Decode a captured S2C packet

from sfs2x import decode_s2c_packet, parse_s2c_command

raw = bytes.fromhex("80001f...")  # paste captured hex
obj, _ = decode_s2c_packet(raw)
cmd, params = parse_s2c_command(obj)
print(f"Command: {cmd}, Params: {params}")

Type-preserving decode for MITM

from sfs2x import decode_c2s_packet_typed

obj, _ = decode_c2s_packet_typed(packet)
# Values are TypedValue instances — re-encoding preserves wire types

Protocol Overview

Wire Format

S2C (server → client):

[0x80] [size: 2B BE] [SFSObject payload]

C2S (client → server):

[0xC4] [server_id: 2B BE] [size: 2B BE] [XOR-obfuscated payload]
[0xE4] [server_id: 2B BE] [size: 2B BE] [XOR(zlib(payload))]  (compressed)

SFSObject Structure

The payload is always a root SFSObject with:

  • "c" → controller ID (BYTE: 0=system, 1=extension)
  • "a" → action ID (SHORT)
  • "p" → parameters (SFS_OBJECT)

Extension commands (c=1, a=13) nest further:

  • "p"."c" → command name (UTF_STRING)
  • "p"."r" → room ID (INT)
  • "p"."p" → command parameters (SFS_OBJECT)

API Reference

sfs2x.objects

  • SFSCodec.encode(obj)bytes — Encode a dict as SFSObject
  • SFSCodec.decode(data)(dict, int) — Decode SFSObject, return (dict, bytes_consumed)
  • SFSCodec.decode_typed(data)(dict, int) — Decode preserving wire types as TypedValue
  • TypedValue(type_id, value) — Force a specific wire type

sfs2x.protocol

  • encode_c2s_packet(controller, action, params, server_id, compress)bytes
  • decode_c2s_packet(data)(dict, int)
  • decode_c2s_packet_typed(data)(dict, int)
  • encode_s2c_packet(obj, compress)bytes
  • decode_s2c_packet(data)(dict, int)
  • make_extension_request(cmd, params, room_id, server_id)bytes
  • make_keepalive(client_time, server_id)bytes
  • parse_s2c_command(obj)(str | None, dict)
  • iter_s2c_packets(data) → iterator of (dict, offset)

sfs2x.crypto

  • AESCipher(key, iv) — AES-128-CBC with fixed IV
  • KeyExchange() — Manages key exchange via CryptoManager endpoint
  • make_password_hash(session_token, password)str — MD5 password hash

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sfs2x_py-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sfs2x_py-0.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file sfs2x_py-0.1.0.tar.gz.

File metadata

  • Download URL: sfs2x_py-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for sfs2x_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9598fcc302966336abc1a764f920e4c6ba73bb8370382698a9ad425affd204b8
MD5 551cdc6d6a6a6aa6f29619da622d311d
BLAKE2b-256 582b6decfd8ea64856f5ce2d83c40c25cf3e18949abd2acf14f51390313995f2

See more details on using hashes here.

File details

Details for the file sfs2x_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sfs2x_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for sfs2x_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82ac1823f923ce1771a79494356962ad8d5a4b8c1732a0e801731054808c9068
MD5 e4bce8034766b9d0d2ce41a4034cd5fa
BLAKE2b-256 867efd13dd5dbff8bd5362e32dc46c7a3a5d8cf77d1e0f478284eb0d13b78f1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page