PHP code example of tourze / shadowsocks-config-php

1. Go to this page and download the library: Download tourze/shadowsocks-config-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / shadowsocks-config-php example snippets


use Shadowsocks\Config\SIP002;

// Create config from JSON file
$sip002 = SIP002::fromJsonFile('/path/to/config.json');
$config = $sip002->getConfig();

// Example JSON config
// {
//    "server":"my_server_ip",
//    "server_port":8388,
//    "local_port":1080,
//    "password":"barfoo!",
//    "method":"chacha20-ietf-poly1305",
//    "plugin":"obfs-local;obfs=http" // Optional
// }

use Shadowsocks\Config\SIP002;

// Create config from standard URI
$sip002 = SIP002::fromUri('ss://bf-cfb:test/!@#:@192.168.100.1:8888#example-server');
$config = $sip002->getConfig();

use Shadowsocks\Config\SIP002;

// Create config from Base64 URI
$sip002 = SIP002::fromBase64Uri('ss://YmYtY2ZiOnRlc3QvIUAjOkAxOTIuMTY4LjEwMC4xOjg4ODg#example-server');
$config = $sip002->getConfig();

use Shadowsocks\Config\SIP002;
use Shadowsocks\Config\ClientConfig;

// Create from SIP002 URI
$sip002 = SIP002::fromUri('ss://[email protected]:8888/?plugin=obfs-local%3Bobfs%3Dhttp#Example');

// Get the config object
$config = $sip002->getConfig();

// Get plugin information
$plugin = $sip002->getPlugin();

// Create a new SIP002 URI
$config = new ClientConfig('192.168.100.1', 8888, 1080, 'password', 'aes-256-gcm');
$sip002 = new SIP002($config);
$sip002->setPlugin('v2ray-plugin;server');

// Generate SIP002 URI
$uri = $sip002->toUri();

use Shadowsocks\Config\SIP008;
use Shadowsocks\Config\ServerConfig;

// Create from SIP008 JSON
$jsonContent = '{
    "version": 1,
    "servers": [
        {
            "id": "27b8a625-4f4b-4428-9f0f-8a2317db7c79",
            "remarks": "Server 1",
            "server": "example1.com",
            "server_port": 8388,
            "password": "password1",
            "method": "aes-256-gcm",
            "plugin": "v2ray-plugin",
            "plugin_opts": "server"
        },
        {
            "id": "7842c068-c667-41f2-8f7d-04feece3cb67",
            "remarks": "Server 2",
            "server": "example2.com",
            "server_port": 8389,
            "password": "password2",
            "method": "chacha20-ietf-poly1305"
        }
    ],
    "bytes_used": 274877906944,
    "bytes_remaining": 824633720832
}';

$sip008 = SIP008::fromJson($jsonContent);

// Or load from URL (HTTPS  from SIP002
$sip002 = new SIP002($config);
$sip002->setPlugin('v2ray-plugin;server');
$sip008 = SIP008::fromSIP002($sip002);

// Create SIP008 from multiple SIP002 objects
$sip002List = [
    new SIP002($config1),
    new SIP002($config2)
];
$sip008 = SIP008::fromSIP002List($sip002List);

// Convert SIP008 to JSON
$jsonString = $sip008->toJson();

use Shadowsocks\Config\ClientConfig;
use Shadowsocks\Config\SIP002;

// Create a config instance directly
$config = new ClientConfig(
    '192.168.100.1',  // Server address
    8888,             // Server port
    1080,             // Local port
    'password123',    // Password
    'aes-256-gcm'     // Encryption method
);

// Set tag
$config->setTag('my-server');

// Wrap with SIP002 for URI generation
$sip002 = new SIP002($config);

use Shadowsocks\Config\ClientConfig;
use Shadowsocks\Config\SIP002;

$config = new ClientConfig('192.168.100.1', 8888, 1080, 'password123', 'aes-256-gcm');
$sip002 = new SIP002($config);

// Convert to JSON config
$jsonConfig = $config->toJson();

// Convert to SIP002 URI (recommended)
$sip002Uri = $sip002->toUri();

// Convert to standard URI format
$standardUri = $sip002->toStandardUri();

// Convert to Base64 encoded URI format
$base64Uri = $sip002->toBase64Uri();