blinksocks/lib/transports/udp.js
2017-10-27 15:08:50 +08:00

1 line
3.0 KiB
JavaScript

'use strict';Object.defineProperty(exports,'__esModule',{value:true});exports.UdpOutbound=exports.UdpInbound=undefined;var _dgram=require('dgram');var _dgram2=_interopRequireDefault(_dgram);var _defs=require('./defs');var _core=require('../core');var _presets=require('../presets');var _utils=require('../utils');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}class UdpInbound extends _defs.Inbound{constructor(props){super(props);this._socket=null;this._rinfo=null;const context=props.context;this.onReceive=this.onReceive.bind(this);this.onPresetFailed=this.onPresetFailed.bind(this);this._socket=context}onReceive(buffer,rinfo){const type=__IS_CLIENT__?_core.PIPE_ENCODE:_core.PIPE_DECODE;this._rinfo=rinfo;this._pipe.feed(type,buffer)}onBroadcast(action){switch(action.type){case _presets.PRESET_FAILED:this.onPresetFailed(action);break;default:break;}}onPresetFailed(action){var _action$payload=action.payload;const name=_action$payload.name,message=_action$payload.message;_utils.logger.error(`[udp:inbound] [${this.remote}] preset "${name}" fail to process: ${message}`);if(this._outbound){this._outbound.destroy();this._outbound=null}this.destroy();this.broadcast({type:_presets.CONNECTION_CLOSED,payload:{host:this.remoteHost,port:this.remotePort}})}write(buffer){var _rinfo=this._rinfo;const address=_rinfo.address,port=_rinfo.port;this._socket.send(buffer,port,address,err=>{if(err){_utils.logger.warn(`[udp:inbound] ${this.remote}:`,err)}})}destroy(){if(this._socket!==null&&this._socket._handle!==null){this._socket.close();this._socket=null;this.emit('close')}}}exports.UdpInbound=UdpInbound;class UdpOutbound extends _defs.Outbound{constructor(props){super(props);this._socket=null;this._targetHost=null;this._targetPort=null;this.onReceive=this.onReceive.bind(this);this._socket=_dgram2.default.createSocket('udp4');this._socket.on('message',this.onReceive)}onReceive(buffer){const type=__IS_CLIENT__?_core.PIPE_DECODE:_core.PIPE_ENCODE;this._pipe.feed(type,buffer)}onBroadcast(action){switch(action.type){case _presets.CONNECT_TO_REMOTE:this.onConnectToRemote(action);break;default:break;}}write(buffer){const host=this._targetHost;const port=this._targetPort;if(host!==null&&port!==null){this._socket.send(buffer,port,host,err=>{if(err){_utils.logger.warn(`[udp:outbound] ${this.remote}:`,err)}})}else{_utils.logger.error('[udp:outbound] fail to send udp data, target address was not initialized.')}}onConnectToRemote(action){var _action$payload2=action.payload;const host=_action$payload2.host,port=_action$payload2.port,onConnected=_action$payload2.onConnected;if(__IS_CLIENT__){this._targetHost=__SERVER_HOST__;this._targetPort=__SERVER_PORT__}if(__IS_SERVER__){this._targetHost=host;this._targetPort=port;if(typeof onConnected==='function'){onConnected()}}_utils.logger.info(`[udp:outbound] [${this.remote}] request: ${host}:${port}`)}destroy(){if(this._socket!==null&&this._socket._handle!==null){this._socket.close();this._socket=null}}}exports.UdpOutbound=UdpOutbound;