Skip to content

Commit

Permalink
Manage folders + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian H-J committed Sep 4, 2017
1 parent adee89c commit 3c66b3e
Show file tree
Hide file tree
Showing 13 changed files with 591 additions and 187 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ slsk.connect({
### slsk
#### connect
##### argument
| key | required | value | note |
|-----|----------|-------|------|
| key | required | value | default | note |
|-----|----------|-------|---------|------|
|user| true |Your username|
|pass| true| Your password|
|host||choose a different host for Slsk server|
|port||choose a different port|
|incomingPort||Port used for incoming connection|For next version|
|incomingPort||Port used for incoming connection||For next version|

##### callback
Return client (see just here ⬇)

### client
#### search
##### argument
|key | value | note |
|-----|-------|------|
|req|Sent to slsk server/peers to search file, use space to add keyword|
|timeout|Slsk doesn't sent when search is finished. We ignore request after this time| Default: 2000ms|
| key | required | value | default | note |
|-----|----------|-------|---------|------|
|req|true|Sent to slsk server/peers to search file, use space to add keyword|
|timeout||Slsk doesn't sent when search is finished. We ignore request after this time|4000|

##### callback

|key | value | note |
|-----|-------|------|
|user|Peer name of slsk|
|file|Full path of peer file|
|freeUpload|Avalaible slots|True if peer have enough slots to get file immediatly|
|freeUpload|Avalaible slots|True if peer have enough slots to get file immediately|

List of files
```json
Expand All @@ -89,4 +89,4 @@ ftp://ftp.tu-clausthal.de/pub/mirror/ftp.gwdg.de/gnu/ftp/savannah/files/mldonkey

https://www.museek-plus.org/wiki/SoulseekProtocol

Nicotine+ software + Wireshark
https://github.com/Nicotine-Plus/nicotine-plus
107 changes: 0 additions & 107 deletions index.js

This file was deleted.

110 changes: 110 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const net = require('net')
const crypto = require('crypto')

const hex = require('hex')
const debug = require('debug')('slsk')

const Message = require('./message.js')
const Messages = require('./messages.js')

let client
let stack = {
search: {}
}

module.exports = {
connect: (obj, cb) => {
initClient(() => {
let timeout = obj.timeout || 2000
setTimeout(() => {
if (stack.login) {
delete stack.login
cb(new Error('timeout login'))
}
}, timeout)
login(obj, err => {
if (err) return cb(err)
cb(null, new SlskClient())
})
})
},
disconnect: () => {
client.destroy()
}
}

class SlskClient {
constructor() {

}

search(obj, cb) {
let msg = new Message()
let token = crypto.randomBytes(4).toString('hex')
msg.int32(26) //code
.rawHexStr(token) //token as int
.str(obj.req) //req

let timeout = obj.timeout || 4000
let results = []
setTimeout(() => {
delete stack.search[token]
cb(null, results)
}, timeout)
stack.search[token] = res => {
results.push(res)
}

client.write(msg.getBuff())
}
}

function initClient(cb) {
debug('Init client')
client = net.createConnection({
host: 'server.slsknet.org',
port: 2242
}, cb)
client.on('data', data => {
debug('data')
//hex(data)
let msgs = new Messages(data)
msgs.forEach(msg => {
let size = msg.int32()
let code = msg.int32()
switch (code) {
case 1: {
let success = msg.int8()
if (success === 1) {
hex(msg.data)
stack.login()
delete stack.login
} else {
let reason = msg.str()
stack.login(new Error(reason))
delete stack.login
}
break
}
default: {
debug(`unknown message code ${code}`)
}
}
})
})
}

function login(obj, cb) {
let msg = new Message()
msg.int32(1) //code
.str(obj.user) //user
.str(obj.pass) //pass
.int32(157) //version
.str(crypto.createHash('md5').update(obj.user + obj.pass).digest('hex')) //hash
.int32(17) //?
.int32(8) //?
.int32(2) //?
.int32(47624) //?
client.write(msg.getBuff())
stack.login = cb
}
Loading

0 comments on commit 3c66b3e

Please sign in to comment.