Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shadowsocks-auth nodejs client
redis = require 'redis'
client = redis.createClient()
SS_PREFIX = 'ss:'
#
# user
# name: shadowsocks 的用户名
# password: shadowsocks 的密码
# method: shadowsocks 的加密方法
# limit: 最大流量
#
# shadowsocks 加密方法有:
# aes-128-cfb, aes-192-cfb, aes-256-cfb, bf-cfb, cast5-cfb, des-cfb, rc4
#
# 注意 redis 要与 shadowsocks-auth 同一个才有效
#
exports.set_user = (user, callback) ->
# 设置 shadowsocks 用户
client.set "#{SS_PREFIX}user:#{user.name}", JSON.stringify(user), callback
exports.get_user = (name, callback) ->
# 获取 shadowsocks 用户信息
client.get "#{SS_PREFIX}user:#{name}", (err, data) ->
return callback err if err
try
data = JSON.parse data
callback null, data
catch e
callback e
exports.del_user = (name, callback) ->
# 删除 shadowsocks 用户
client.del "#{SS_PREFIX}user:#{name}", callback
exports.get_used_flow = (name, callback) ->
# 获取当前用户使用的流量
client.get "#{SS_PREFIX}flow:#{name}", callback
exports.set_used_flow = (name, flow, callback) ->
# 设置当前用户使用的流量
client.set "#{SS_PREFIX}flow:#{name}", flow, callback
exports.del_used_flow = (name, callback) ->
# 删除当前用户使用的流量
client.del "#{SS_PREFIX}flow:#{name}", callback
exports.flow = (opts, callback) ->
# 获取用户流量使用的列表
# opts
# name
# year: 2014
# month
# day
# member
# start: 0
# stop: 20
#
# member 为 host:port 或者 total
# 当 member 存在时只返回流量值
#
opts.year = opts.year or '2014'
key = "#{SS_PREFIX}flow:#{opts.name}:#{opts.year}"
key = "#{key}:#{opts.month}" if opts.month
key = "#{key}:#{opts.day}" if opts.day
return client.zscore key, member, callback if opts.member
opts.start = opts.start or 0
opts.stop = opts.stop or 20
client.zrevrange key, opts.start, opts.stop, 'withscores', callback
@mohamad77

This comment has been minimized.

Copy link

commented Mar 16, 2016

how can i use it?

@Lupino

This comment has been minimized.

Copy link
Owner Author

commented Apr 14, 2016

// install requirement
// npm install redis
var ss = require('ss');
// create a new user.
var user = {
  name: 'name',
  password: 'password',
  method: 'aes-128-cfb',
  limit: 1102410241024
};

ss.set_user(user, function() {
   console.log('create user done');
});
@wehaha

This comment has been minimized.

Copy link

commented Aug 17, 2017

现在还能用吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.