@@ -1,3 +1,41 @@ | |||
2.5.6 2015-08-19 | |||
- Add portable mode. Create shadowsocks_portable_mode.txt to use it | |||
- Support server reorder | |||
2.5.5 2015-08-17 | |||
- Fix crash when enabling Availability Statistics and some servers can not be resolved | |||
- Allow multiple instances | |||
- Other fixes | |||
2.5.4 2015-08-16 | |||
- Hide Privoxy icon | |||
2.5.3 2015-08-16 | |||
- Replace Polipo with Privoxy | |||
- Add Choose by Total Packet Loss | |||
2.5.2 2015-08-04 | |||
- Add log viewer | |||
2.5.1 2015-07-26 | |||
- Prevent HA from switching servers too frequently | |||
- Fix server settings can not be updated when using HA | |||
- Fix server port can't be 8123 | |||
- Other minor fixes | |||
2.5 2015-07-25 | |||
- Support load balance | |||
- Support high availability | |||
2.4 2015-07-11 | |||
- Support UDP relay | |||
- Support online PAC | |||
- Migrate update checker to GitHub releases | |||
- Other fixes | |||
2.3.1 2015-03-06 | |||
- Support user rule | |||
2.3 2015-01-25 | |||
- Use the same port for every profile | |||
- Use the same port for HTTP/Socks5/PAC | |||
@@ -20,30 +20,348 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
3rd party projects | |||
================== | |||
polipo | |||
Privoxy | |||
------------------ | |||
https://github.com/jech/polipo | |||
Copyright (c) 2003-2008 by Juliusz Chroboczek | |||
Permission is hereby granted, free of charge, to any person obtaining a copy | |||
of this software and associated documentation files (the "Software"), to deal | |||
in the Software without restriction, including without limitation the rights | |||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
copies of the Software, and to permit persons to whom the Software is | |||
furnished to do so, subject to the following conditions: | |||
The above copyright notice and this permission notice shall be included in | |||
all copies or substantial portions of the Software. | |||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |||
THE SOFTWARE. | |||
GNU GENERAL PUBLIC LICENSE | |||
Version 2, June 1991 | |||
Copyright (C) 1989, 1991 Free Software Foundation, Inc., | |||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |||
Everyone is permitted to copy and distribute verbatim copies | |||
of this license document, but changing it is not allowed. | |||
Preamble | |||
The licenses for most software are designed to take away your | |||
freedom to share and change it. By contrast, the GNU General Public | |||
License is intended to guarantee your freedom to share and change free | |||
software--to make sure the software is free for all its users. This | |||
General Public License applies to most of the Free Software | |||
Foundation's software and to any other program whose authors commit to | |||
using it. (Some other Free Software Foundation software is covered by | |||
the GNU Lesser General Public License instead.) You can apply it to | |||
your programs, too. | |||
When we speak of free software, we are referring to freedom, not | |||
price. Our General Public Licenses are designed to make sure that you | |||
have the freedom to distribute copies of free software (and charge for | |||
this service if you wish), that you receive source code or can get it | |||
if you want it, that you can change the software or use pieces of it | |||
in new free programs; and that you know you can do these things. | |||
To protect your rights, we need to make restrictions that forbid | |||
anyone to deny you these rights or to ask you to surrender the rights. | |||
These restrictions translate to certain responsibilities for you if you | |||
distribute copies of the software, or if you modify it. | |||
For example, if you distribute copies of such a program, whether | |||
gratis or for a fee, you must give the recipients all the rights that | |||
you have. You must make sure that they, too, receive or can get the | |||
source code. And you must show them these terms so they know their | |||
rights. | |||
We protect your rights with two steps: (1) copyright the software, and | |||
(2) offer you this license which gives you legal permission to copy, | |||
distribute and/or modify the software. | |||
Also, for each author's protection and ours, we want to make certain | |||
that everyone understands that there is no warranty for this free | |||
software. If the software is modified by someone else and passed on, we | |||
want its recipients to know that what they have is not the original, so | |||
that any problems introduced by others will not reflect on the original | |||
authors' reputations. | |||
Finally, any free program is threatened constantly by software | |||
patents. We wish to avoid the danger that redistributors of a free | |||
program will individually obtain patent licenses, in effect making the | |||
program proprietary. To prevent this, we have made it clear that any | |||
patent must be licensed for everyone's free use or not licensed at all. | |||
The precise terms and conditions for copying, distribution and | |||
modification follow. | |||
GNU GENERAL PUBLIC LICENSE | |||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |||
0. This License applies to any program or other work which contains | |||
a notice placed by the copyright holder saying it may be distributed | |||
under the terms of this General Public License. The "Program", below, | |||
refers to any such program or work, and a "work based on the Program" | |||
means either the Program or any derivative work under copyright law: | |||
that is to say, a work containing the Program or a portion of it, | |||
either verbatim or with modifications and/or translated into another | |||
language. (Hereinafter, translation is included without limitation in | |||
the term "modification".) Each licensee is addressed as "you". | |||
Activities other than copying, distribution and modification are not | |||
covered by this License; they are outside its scope. The act of | |||
running the Program is not restricted, and the output from the Program | |||
is covered only if its contents constitute a work based on the | |||
Program (independent of having been made by running the Program). | |||
Whether that is true depends on what the Program does. | |||
1. You may copy and distribute verbatim copies of the Program's | |||
source code as you receive it, in any medium, provided that you | |||
conspicuously and appropriately publish on each copy an appropriate | |||
copyright notice and disclaimer of warranty; keep intact all the | |||
notices that refer to this License and to the absence of any warranty; | |||
and give any other recipients of the Program a copy of this License | |||
along with the Program. | |||
You may charge a fee for the physical act of transferring a copy, and | |||
you may at your option offer warranty protection in exchange for a fee. | |||
2. You may modify your copy or copies of the Program or any portion | |||
of it, thus forming a work based on the Program, and copy and | |||
distribute such modifications or work under the terms of Section 1 | |||
above, provided that you also meet all of these conditions: | |||
a) You must cause the modified files to carry prominent notices | |||
stating that you changed the files and the date of any change. | |||
b) You must cause any work that you distribute or publish, that in | |||
whole or in part contains or is derived from the Program or any | |||
part thereof, to be licensed as a whole at no charge to all third | |||
parties under the terms of this License. | |||
c) If the modified program normally reads commands interactively | |||
when run, you must cause it, when started running for such | |||
interactive use in the most ordinary way, to print or display an | |||
announcement including an appropriate copyright notice and a | |||
notice that there is no warranty (or else, saying that you provide | |||
a warranty) and that users may redistribute the program under | |||
these conditions, and telling the user how to view a copy of this | |||
License. (Exception: if the Program itself is interactive but | |||
does not normally print such an announcement, your work based on | |||
the Program is not required to print an announcement.) | |||
These requirements apply to the modified work as a whole. If | |||
identifiable sections of that work are not derived from the Program, | |||
and can be reasonably considered independent and separate works in | |||
themselves, then this License, and its terms, do not apply to those | |||
sections when you distribute them as separate works. But when you | |||
distribute the same sections as part of a whole which is a work based | |||
on the Program, the distribution of the whole must be on the terms of | |||
this License, whose permissions for other licensees extend to the | |||
entire whole, and thus to each and every part regardless of who wrote it. | |||
Thus, it is not the intent of this section to claim rights or contest | |||
your rights to work written entirely by you; rather, the intent is to | |||
exercise the right to control the distribution of derivative or | |||
collective works based on the Program. | |||
In addition, mere aggregation of another work not based on the Program | |||
with the Program (or with a work based on the Program) on a volume of | |||
a storage or distribution medium does not bring the other work under | |||
the scope of this License. | |||
3. You may copy and distribute the Program (or a work based on it, | |||
under Section 2) in object code or executable form under the terms of | |||
Sections 1 and 2 above provided that you also do one of the following: | |||
a) Accompany it with the complete corresponding machine-readable | |||
source code, which must be distributed under the terms of Sections | |||
1 and 2 above on a medium customarily used for software interchange; or, | |||
b) Accompany it with a written offer, valid for at least three | |||
years, to give any third party, for a charge no more than your | |||
cost of physically performing source distribution, a complete | |||
machine-readable copy of the corresponding source code, to be | |||
distributed under the terms of Sections 1 and 2 above on a medium | |||
customarily used for software interchange; or, | |||
c) Accompany it with the information you received as to the offer | |||
to distribute corresponding source code. (This alternative is | |||
allowed only for noncommercial distribution and only if you | |||
received the program in object code or executable form with such | |||
an offer, in accord with Subsection b above.) | |||
The source code for a work means the preferred form of the work for | |||
making modifications to it. For an executable work, complete source | |||
code means all the source code for all modules it contains, plus any | |||
associated interface definition files, plus the scripts used to | |||
control compilation and installation of the executable. However, as a | |||
special exception, the source code distributed need not include | |||
anything that is normally distributed (in either source or binary | |||
form) with the major components (compiler, kernel, and so on) of the | |||
operating system on which the executable runs, unless that component | |||
itself accompanies the executable. | |||
If distribution of executable or object code is made by offering | |||
access to copy from a designated place, then offering equivalent | |||
access to copy the source code from the same place counts as | |||
distribution of the source code, even though third parties are not | |||
compelled to copy the source along with the object code. | |||
4. You may not copy, modify, sublicense, or distribute the Program | |||
except as expressly provided under this License. Any attempt | |||
otherwise to copy, modify, sublicense or distribute the Program is | |||
void, and will automatically terminate your rights under this License. | |||
However, parties who have received copies, or rights, from you under | |||
this License will not have their licenses terminated so long as such | |||
parties remain in full compliance. | |||
5. You are not required to accept this License, since you have not | |||
signed it. However, nothing else grants you permission to modify or | |||
distribute the Program or its derivative works. These actions are | |||
prohibited by law if you do not accept this License. Therefore, by | |||
modifying or distributing the Program (or any work based on the | |||
Program), you indicate your acceptance of this License to do so, and | |||
all its terms and conditions for copying, distributing or modifying | |||
the Program or works based on it. | |||
6. Each time you redistribute the Program (or any work based on the | |||
Program), the recipient automatically receives a license from the | |||
original licensor to copy, distribute or modify the Program subject to | |||
these terms and conditions. You may not impose any further | |||
restrictions on the recipients' exercise of the rights granted herein. | |||
You are not responsible for enforcing compliance by third parties to | |||
this License. | |||
7. If, as a consequence of a court judgment or allegation of patent | |||
infringement or for any other reason (not limited to patent issues), | |||
conditions are imposed on you (whether by court order, agreement or | |||
otherwise) that contradict the conditions of this License, they do not | |||
excuse you from the conditions of this License. If you cannot | |||
distribute so as to satisfy simultaneously your obligations under this | |||
License and any other pertinent obligations, then as a consequence you | |||
may not distribute the Program at all. For example, if a patent | |||
license would not permit royalty-free redistribution of the Program by | |||
all those who receive copies directly or indirectly through you, then | |||
the only way you could satisfy both it and this License would be to | |||
refrain entirely from distribution of the Program. | |||
If any portion of this section is held invalid or unenforceable under | |||
any particular circumstance, the balance of the section is intended to | |||
apply and the section as a whole is intended to apply in other | |||
circumstances. | |||
It is not the purpose of this section to induce you to infringe any | |||
patents or other property right claims or to contest validity of any | |||
such claims; this section has the sole purpose of protecting the | |||
integrity of the free software distribution system, which is | |||
implemented by public license practices. Many people have made | |||
generous contributions to the wide range of software distributed | |||
through that system in reliance on consistent application of that | |||
system; it is up to the author/donor to decide if he or she is willing | |||
to distribute software through any other system and a licensee cannot | |||
impose that choice. | |||
This section is intended to make thoroughly clear what is believed to | |||
be a consequence of the rest of this License. | |||
8. If the distribution and/or use of the Program is restricted in | |||
certain countries either by patents or by copyrighted interfaces, the | |||
original copyright holder who places the Program under this License | |||
may add an explicit geographical distribution limitation excluding | |||
those countries, so that distribution is permitted only in or among | |||
countries not thus excluded. In such case, this License incorporates | |||
the limitation as if written in the body of this License. | |||
9. The Free Software Foundation may publish revised and/or new versions | |||
of the General Public License from time to time. Such new versions will | |||
be similar in spirit to the present version, but may differ in detail to | |||
address new problems or concerns. | |||
Each version is given a distinguishing version number. If the Program | |||
specifies a version number of this License which applies to it and "any | |||
later version", you have the option of following the terms and conditions | |||
either of that version or of any later version published by the Free | |||
Software Foundation. If the Program does not specify a version number of | |||
this License, you may choose any version ever published by the Free Software | |||
Foundation. | |||
10. If you wish to incorporate parts of the Program into other free | |||
programs whose distribution conditions are different, write to the author | |||
to ask for permission. For software which is copyrighted by the Free | |||
Software Foundation, write to the Free Software Foundation; we sometimes | |||
make exceptions for this. Our decision will be guided by the two goals | |||
of preserving the free status of all derivatives of our free software and | |||
of promoting the sharing and reuse of software generally. | |||
NO WARRANTY | |||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY | |||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN | |||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES | |||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED | |||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS | |||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE | |||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, | |||
REPAIR OR CORRECTION. | |||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | |||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR | |||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, | |||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING | |||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED | |||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY | |||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER | |||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | |||
POSSIBILITY OF SUCH DAMAGES. | |||
END OF TERMS AND CONDITIONS | |||
How to Apply These Terms to Your New Programs | |||
If you develop a new program, and you want it to be of the greatest | |||
possible use to the public, the best way to achieve this is to make it | |||
free software which everyone can redistribute and change under these terms. | |||
To do so, attach the following notices to the program. It is safest | |||
to attach them to the start of each source file to most effectively | |||
convey the exclusion of warranty; and each file should have at least | |||
the "copyright" line and a pointer to where the full notice is found. | |||
<one line to give the program's name and a brief idea of what it does.> | |||
Copyright (C) <year> <name of author> | |||
This program is free software; you can redistribute it and/or modify | |||
it under the terms of the GNU General Public License as published by | |||
the Free Software Foundation; either version 2 of the License, or | |||
(at your option) any later version. | |||
This program is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
GNU General Public License for more details. | |||
You should have received a copy of the GNU General Public License along | |||
with this program; if not, write to the Free Software Foundation, Inc., | |||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||
Also add information on how to contact you by electronic and paper mail. | |||
If the program is interactive, make it output a short notice like this | |||
when it starts in an interactive mode: | |||
Gnomovision version 69, Copyright (C) year name of author | |||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | |||
This is free software, and you are welcome to redistribute it | |||
under certain conditions; type `show c' for details. | |||
The hypothetical commands `show w' and `show c' should show the appropriate | |||
parts of the General Public License. Of course, the commands you use may | |||
be called something other than `show w' and `show c'; they could even be | |||
mouse-clicks or menu items--whatever suits your program. | |||
You should also get your employer (if you work as a programmer) or your | |||
school, if any, to sign a "copyright disclaimer" for the program, if | |||
necessary. Here is a sample; alter the names: | |||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program | |||
`Gnomovision' (which makes passes at compilers) written by James Hacker. | |||
<signature of Ty Coon>, 1 April 1989 | |||
Ty Coon, President of Vice | |||
This General Public License does not permit incorporating your program into | |||
proprietary programs. If your program is a subroutine library, you may | |||
consider it more useful to permit linking proprietary applications with the | |||
library. If this is what you want to do, use the GNU Lesser General | |||
Public License instead of this License. | |||
PolarSSL | |||
@@ -98,7 +416,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
See the License for the specific language governing permissions and | |||
limitations under the License. | |||
libsodium | |||
--------- | |||
@@ -1,34 +1,83 @@ | |||
Shadowsocks for Windows | |||
======================= | |||
#### Features compare with official version | |||
[![Build Status]][Appveyor] | |||
1. Add a speed test form | |||
2. Get server location | |||
3. Download speed test | |||
[中文说明] | |||
#### 和官方版本相比有什么不同 | |||
#### Features | |||
1. 新增了一个测速的功能 | |||
2. 可以获取获取服务器IP归属地,如果不想联网获取,请下载[纯真IP库]并把qqwry.dat文件放置在当前目录 | |||
3. 下载速度测试(通过下载谷歌浏览器的安装器来测试,大概900多K) | |||
1. System proxy configuration | |||
2. PAC mode and global mode | |||
3. [GFWList] and user rules | |||
4. Supports HTTP proxy | |||
5. Supports server auto switching | |||
6. Supports UDP relay (see Usage) | |||
#### Download | |||
Download [latest release]. | |||
Download the [latest release]. | |||
#### Usage | |||
#### Basic | |||
See [official] | |||
1. Find Shadowsocks icon in the notification tray | |||
2. You can add multiple servers in servers menu | |||
3. Select `Enable System Proxy` menu to enable system proxy. Please disable other | |||
proxy addons in your browser, or set them to use system proxy | |||
4. You can also configure your browser proxy manually if you don't want to enable | |||
system proxy. Set Socks5 or HTTP proxy to 127.0.0.1:1080. You can change this | |||
port in `Servers -> Edit Servers` | |||
### Develop | |||
#### PAC | |||
Visual Studio 2015 is recommended. | |||
1. You can change PAC rules by editing the PAC file. When you save the PAC file | |||
with any editor, Shadowsocks will notify browsers about the change automatically | |||
2. You can also update PAC file from [GFWList] (maintained by 3rd party) | |||
3. You can also use online PAC URL | |||
#### Server Auto Switching | |||
1. Load balance: choosing server randomly | |||
2. High availability: choosing the best server (low latency and packet loss) | |||
3. Choose By Total Package Loss: ping and choose. Please also enable | |||
`Availability Statistics` in the menu if you want to use this | |||
4. Write your own strategy by implement IStrategy interface and send us a pull request! | |||
#### UDP | |||
For UDP, you need to use SocksCap or ProxyCap to force programs you want | |||
to be proxied to tunnel over Shadowsocks | |||
#### Multiple Instances | |||
If you want to manage multiple servers using other tools like SwitchyOmega, | |||
you can start multiple Shadowsocks instances. To avoid configuration conflicts, | |||
copy Shadowsocks to a new directory and choose a different local port. | |||
Also, make sure to use `SOCKS5` proxy in SwitchyOmega, since we have only | |||
one HTTP proxy instance. | |||
#### Server Configuration | |||
Please visit [Servers] for more information. | |||
#### Portable Mode | |||
If you want to put all temporary files into shadowsocks/temp folder instead of | |||
system temp folder, create a `shadowsocks_portable_mode.txt` into shadowsocks folder. | |||
#### Develop | |||
Visual Studio 2015 is required. | |||
#### License | |||
GPLv3 | |||
[latest release]: https://github.com/TkYu/shadowsocks-csharp-withping/releases | |||
[official]: https://github.com/shadowsocks/shadowsocks-csharp | |||
[纯真IP库]: http://update.cz88.net/soft/setup.zip | |||
[Appveyor]: https://ci.appveyor.com/project/clowwindy/shadowsocks-csharp | |||
[Build Status]: https://ci.appveyor.com/api/projects/status/gknc8l1lxy423ehv/branch/master | |||
[latest release]: https://github.com/shadowsocks/shadowsocks-csharp/releases | |||
[GFWList]: https://github.com/gfwlist/gfwlist | |||
[Servers]: https://github.com/shadowsocks/shadowsocks/wiki/Ports-and-Clients#linux--server-side | |||
[中文说明]: https://github.com/shadowsocks/shadowsocks-windows/wiki/Shadowsocks-Windows-%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E |
@@ -1,13 +1,11 @@ | |||
| |||
Microsoft Visual Studio Solution File, Format Version 12.00 | |||
# Visual Studio 14 | |||
VisualStudioVersion = 14.0.22823.1 | |||
MinimumVisualStudioVersion = 10.0.40219.1 | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp", "shadowsocks-csharp\shadowsocks-csharp.csproj", "{F58374A5-9AFB-430A-AF20-C509D3DCED3F}" | |||
# Visual Studio Express 2012 for Windows Desktop | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp", "shadowsocks-csharp\shadowsocks-csharp.csproj", "{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}" | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{45913187-0685-4903-B250-DCEF0479CD86}" | |||
ProjectSection(ProjectDependencies) = postProject | |||
{F58374A5-9AFB-430A-AF20-C509D3DCED3F} = {F58374A5-9AFB-430A-AF20-C509D3DCED3F} | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} = {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} | |||
EndProjectSection | |||
EndProject | |||
Global | |||
@@ -16,10 +14,11 @@ Global | |||
Release|x86 = Release|x86 | |||
EndGlobalSection | |||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | |||
{F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Debug|x86.ActiveCfg = Debug|Any CPU | |||
{F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Debug|x86.Build.0 = Debug|Any CPU | |||
{F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Release|x86.ActiveCfg = Release|Any CPU | |||
{F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Release|x86.Build.0 = Release|Any CPU | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.ActiveCfg = Debug|x86 | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Build.0 = Debug|x86 | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Deploy.0 = Debug|x86 | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.ActiveCfg = Release|x86 | |||
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.Build.0 = Release|x86 | |||
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.ActiveCfg = Debug|x86 | |||
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.Build.0 = Debug|x86 | |||
{45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.ActiveCfg = Release|x86 | |||
@@ -1,113 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// Implements a SOCKS authentication scheme. | |||
/// </summary> | |||
/// <remarks>This is an abstract class; it must be inherited.</remarks> | |||
internal abstract class AuthMethod { | |||
/// <summary> | |||
/// Initializes an AuthMethod instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
public AuthMethod(Socket server) { | |||
Server = server; | |||
} | |||
/// <summary> | |||
/// Authenticates the user. | |||
/// </summary> | |||
/// <exception cref="ProxyException">Authentication with the proxy server failed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public abstract void Authenticate(); | |||
/// <summary> | |||
/// Authenticates the user asynchronously. | |||
/// </summary> | |||
/// <param name="callback">The method to call when the authentication is complete.</param> | |||
/// <exception cref="ProxyException">Authentication with the proxy server failed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public abstract void BeginAuthenticate(HandShakeComplete callback); | |||
/// <summary> | |||
/// Gets or sets the socket connection with the proxy server. | |||
/// </summary> | |||
/// <value>The socket connection with the proxy server.</value> | |||
protected Socket Server { | |||
get { | |||
return m_Server; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Server = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets a byt array that can be used to store data. | |||
/// </summary> | |||
/// <value>A byte array to store data.</value> | |||
protected byte[] Buffer { | |||
get { | |||
return m_Buffer; | |||
} | |||
set { | |||
m_Buffer = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the number of bytes that have been received from the remote proxy server. | |||
/// </summary> | |||
/// <value>An integer that holds the number of bytes that have been received from the remote proxy server.</value> | |||
protected int Received { | |||
get { | |||
return m_Received; | |||
} | |||
set { | |||
m_Received = value; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Holds the value of the Buffer property.</summary> | |||
private byte[] m_Buffer; | |||
/// <summary>Holds the value of the Server property.</summary> | |||
private Socket m_Server; | |||
/// <summary>Holds the address of the method to call when the proxy has authenticated the client.</summary> | |||
protected HandShakeComplete CallBack; | |||
/// <summary>Holds the value of the Received property.</summary> | |||
private int m_Received; | |||
} | |||
} |
@@ -1,58 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System.Net.Sockets; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// This class implements the 'No Authentication' scheme. | |||
/// </summary> | |||
internal sealed class AuthNone : AuthMethod { | |||
/// <summary> | |||
/// Initializes an AuthNone instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
public AuthNone(Socket server) : base(server) {} | |||
/// <summary> | |||
/// Authenticates the user. | |||
/// </summary> | |||
public override void Authenticate() { | |||
return; // Do Nothing | |||
} | |||
/// <summary> | |||
/// Authenticates the user asynchronously. | |||
/// </summary> | |||
/// <param name="callback">The method to call when the authentication is complete.</param> | |||
/// <remarks>This method immediately calls the callback method.</remarks> | |||
public override void BeginAuthenticate(HandShakeComplete callback) { | |||
callback(null); | |||
} | |||
} | |||
} |
@@ -1,156 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// This class implements the 'username/password authentication' scheme. | |||
/// </summary> | |||
internal sealed class AuthUserPass : AuthMethod { | |||
/// <summary> | |||
/// Initializes a new AuthUserPass instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <param name="user">The username to use.</param> | |||
/// <param name="pass">The password to use.</param> | |||
/// <exception cref="ArgumentNullException"><c>user</c> -or- <c>pass</c> is null.</exception> | |||
public AuthUserPass(Socket server, string user, string pass) : base(server) { | |||
Username = user; | |||
Password = pass; | |||
} | |||
/// <summary> | |||
/// Creates an array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme. | |||
/// </summary> | |||
/// <returns>An array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme.</returns> | |||
private byte[] GetAuthenticationBytes() { | |||
byte[] buffer = new byte[3 + Username.Length + Password.Length]; | |||
buffer[0] = 1; | |||
buffer[1] = (byte)Username.Length; | |||
Array.Copy(Encoding.ASCII.GetBytes(Username), 0, buffer, 2, Username.Length); | |||
buffer[Username.Length + 2] = (byte)Password.Length; | |||
Array.Copy(Encoding.ASCII.GetBytes(Password), 0, buffer, Username.Length + 3, Password.Length); | |||
return buffer; | |||
} | |||
/// <summary> | |||
/// Starts the authentication process. | |||
/// </summary> | |||
public override void Authenticate() { | |||
Server.Send(GetAuthenticationBytes()); | |||
byte[] buffer = new byte[2]; | |||
int received = 0; | |||
while (received != 2) { | |||
received += Server.Receive(buffer, received, 2 - received, SocketFlags.None); | |||
} | |||
if (buffer[1] != 0) { | |||
Server.Close(); | |||
throw new ProxyException("Username/password combination rejected."); | |||
} | |||
return; | |||
} | |||
/// <summary> | |||
/// Starts the asynchronous authentication process. | |||
/// </summary> | |||
/// <param name="callback">The method to call when the authentication is complete.</param> | |||
public override void BeginAuthenticate(HandShakeComplete callback) { | |||
CallBack = callback; | |||
Server.BeginSend(GetAuthenticationBytes(), 0, 3 + Username.Length + Password.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); | |||
return; | |||
} | |||
/// <summary> | |||
/// Called when the authentication bytes have been sent. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnSent(IAsyncResult ar) { | |||
try { | |||
Server.EndSend(ar); | |||
Buffer = new byte[2]; | |||
Server.BeginReceive(Buffer, 0, 2, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} catch (Exception e) { | |||
CallBack(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the socket received an authentication reply. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnReceive(IAsyncResult ar) { | |||
try { | |||
Received += Server.EndReceive(ar); | |||
if (Received == Buffer.Length) | |||
if (Buffer[1] == 0) | |||
CallBack(null); | |||
else | |||
throw new ProxyException("Username/password combination not accepted."); | |||
else | |||
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} catch (Exception e) { | |||
CallBack(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the username to use when authenticating with the proxy server. | |||
/// </summary> | |||
/// <value>The username to use when authenticating with the proxy server.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
private string Username { | |||
get { | |||
return m_Username; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Username = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the password to use when authenticating with the proxy server. | |||
/// </summary> | |||
/// <value>The password to use when authenticating with the proxy server.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
private string Password { | |||
get { | |||
return m_Password; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Password = value; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Holds the value of the Username property.</summary> | |||
private string m_Username; | |||
/// <summary>Holds the value of the Password property.</summary> | |||
private string m_Password; | |||
} | |||
} |
@@ -1,96 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Threading; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// A class that implements the IAsyncResult interface. Objects from this class are returned by the BeginConnect method of the ProxySocket class. | |||
/// </summary> | |||
internal class IAsyncProxyResult : IAsyncResult { | |||
/// <summary>Initializes the internal variables of this object</summary> | |||
/// <param name="stateObject">An object that contains state information for this request.</param> | |||
internal void Init(object stateObject) { | |||
m_StateObject = stateObject; | |||
m_Completed = false; | |||
if (m_WaitHandle != null) | |||
m_WaitHandle.Reset(); | |||
} | |||
/// <summary>Initializes the internal variables of this object</summary> | |||
internal void Reset() { | |||
m_StateObject = null; | |||
m_Completed = true; | |||
if (m_WaitHandle != null) | |||
m_WaitHandle.Set(); | |||
} | |||
/// <summary>Gets a value that indicates whether the server has completed processing the call. It is illegal for the server to use any client supplied resources outside of the agreed upon sharing semantics after it sets the IsCompleted property to "true". Thus, it is safe for the client to destroy the resources after IsCompleted property returns "true".</summary> | |||
/// <value>A boolean that indicates whether the server has completed processing the call.</value> | |||
public bool IsCompleted { | |||
get { | |||
return m_Completed; | |||
} | |||
} | |||
/// <summary>Gets a value that indicates whether the BeginXXXX call has been completed synchronously. If this is detected in the AsyncCallback delegate, it is probable that the thread that called BeginInvoke is the current thread.</summary> | |||
/// <value>Returns false.</value> | |||
public bool CompletedSynchronously { | |||
get { | |||
return false; | |||
} | |||
} | |||
/// <summary>Gets an object that was passed as the state parameter of the BeginXXXX method call.</summary> | |||
/// <value>The object that was passed as the state parameter of the BeginXXXX method call.</value> | |||
public object AsyncState { | |||
get { | |||
return m_StateObject; | |||
} | |||
} | |||
/// <summary> | |||
/// The AsyncWaitHandle property returns the WaitHandle that can use to perform a WaitHandle.WaitOne or WaitAny or WaitAll. The object which implements IAsyncResult need not derive from the System.WaitHandle classes directly. The WaitHandle wraps its underlying synchronization primitive and should be signaled after the call is completed. This enables the client to wait for the call to complete instead polling. The Runtime supplies a number of waitable objects that mirror Win32 synchronization primitives e.g. ManualResetEvent, AutoResetEvent and Mutex. | |||
/// WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with "any" or "all" semantics i.e. WaitHandle.WaitOne, WaitAny and WaitAll. Such methods are context aware to avoid deadlocks. The AsyncWaitHandle can be allocated eagerly or on demand. It is the choice of the IAsyncResult implementer. | |||
///</summary> | |||
/// <value>The WaitHandle associated with this asynchronous result.</value> | |||
public WaitHandle AsyncWaitHandle { | |||
get { | |||
if (m_WaitHandle == null) | |||
m_WaitHandle = new ManualResetEvent(false); | |||
return m_WaitHandle; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Used internally to represent the state of the asynchronous request</summary> | |||
internal bool m_Completed = true; | |||
/// <summary>Holds the value of the StateObject property.</summary> | |||
private object m_StateObject; | |||
/// <summary>Holds the value of the WaitHandle property.</summary> | |||
private ManualResetEvent m_WaitHandle; | |||
} | |||
} |
@@ -1,82 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// The exception that is thrown when a proxy error occurs. | |||
/// </summary> | |||
public class ProxyException : Exception { | |||
/// <summary> | |||
/// Initializes a new instance of the ProxyException class. | |||
/// </summary> | |||
public ProxyException() : this("An error occured while talking to the proxy server.") {} | |||
/// <summary> | |||
/// Initializes a new instance of the ProxyException class. | |||
/// </summary> | |||
/// <param name="message">The message that describes the error.</param> | |||
public ProxyException(string message) : base(message) {} | |||
/// <summary> | |||
/// Initializes a new instance of the ProxyException class. | |||
/// </summary> | |||
/// <param name="socks5Error">The error number returned by a SOCKS5 server.</param> | |||
public ProxyException(int socks5Error) : this(ProxyException.Socks5ToString(socks5Error)) {} | |||
/// <summary> | |||
/// Converts a SOCKS5 error number to a human readable string. | |||
/// </summary> | |||
/// <param name="socks5Error">The error number returned by a SOCKS5 server.</param> | |||
/// <returns>A string representation of the specified SOCKS5 error number.</returns> | |||
public static string Socks5ToString(int socks5Error) { | |||
switch(socks5Error) { | |||
case 0: | |||
return "Connection succeeded."; | |||
case 1: | |||
return "General SOCKS server failure."; | |||
case 2: | |||
return "Connection not allowed by ruleset."; | |||
case 3: | |||
return "Network unreachable."; | |||
case 4: | |||
return "Host unreachable."; | |||
case 5: | |||
return "Connection refused."; | |||
case 6: | |||
return "TTL expired."; | |||
case 7: | |||
return "Command not supported."; | |||
case 8: | |||
return "Address type not supported."; | |||
default: | |||
return "Unspecified SOCKS error."; | |||
} | |||
} | |||
} | |||
} |
@@ -1,385 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
// Implements a number of classes to allow Sockets to connect trough a firewall. | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// Specifies the type of proxy servers that an instance of the ProxySocket class can use. | |||
/// </summary> | |||
public enum ProxyTypes { | |||
/// <summary>No proxy server; the ProxySocket object behaves exactly like an ordinary Socket object.</summary> | |||
None, | |||
/// <summary>A SOCKS4[A] proxy server.</summary> | |||
Socks4, | |||
/// <summary>A SOCKS5 proxy server.</summary> | |||
Socks5 | |||
} | |||
/// <summary> | |||
/// Implements a Socket class that can connect trough a SOCKS proxy server. | |||
/// </summary> | |||
/// <remarks>This class implements SOCKS4[A] and SOCKS5.<br>It does not, however, implement the BIND commands, so you cannot .</br></remarks> | |||
public class ProxySocket : Socket { | |||
/// <summary> | |||
/// Initializes a new instance of the ProxySocket class. | |||
/// </summary> | |||
/// <param name="addressFamily">One of the AddressFamily values.</param> | |||
/// <param name="socketType">One of the SocketType values.</param> | |||
/// <param name="protocolType">One of the ProtocolType values.</param> | |||
/// <exception cref="SocketException">The combination of addressFamily, socketType, and protocolType results in an invalid socket.</exception> | |||
public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) : this(addressFamily, socketType, protocolType, "") {} | |||
/// <summary> | |||
/// Initializes a new instance of the ProxySocket class. | |||
/// </summary> | |||
/// <param name="addressFamily">One of the AddressFamily values.</param> | |||
/// <param name="socketType">One of the SocketType values.</param> | |||
/// <param name="protocolType">One of the ProtocolType values.</param> | |||
/// <param name="proxyUsername">The username to use when authenticating with the proxy server.</param> | |||
/// <exception cref="SocketException">The combination of addressFamily, socketType, and protocolType results in an invalid socket.</exception> | |||
/// <exception cref="ArgumentNullException"><c>proxyUsername</c> is null.</exception> | |||
public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername) : this(addressFamily, socketType, protocolType, proxyUsername, "") {} | |||
/// <summary> | |||
/// Initializes a new instance of the ProxySocket class. | |||
/// </summary> | |||
/// <param name="addressFamily">One of the AddressFamily values.</param> | |||
/// <param name="socketType">One of the SocketType values.</param> | |||
/// <param name="protocolType">One of the ProtocolType values.</param> | |||
/// <param name="proxyUsername">The username to use when authenticating with the proxy server.</param> | |||
/// <param name="proxyPassword">The password to use when authenticating with the proxy server.</param> | |||
/// <exception cref="SocketException">The combination of addressFamily, socketType, and protocolType results in an invalid socket.</exception> | |||
/// <exception cref="ArgumentNullException"><c>proxyUsername</c> -or- <c>proxyPassword</c> is null.</exception> | |||
public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername, string proxyPassword) : base(addressFamily, socketType, protocolType) { | |||
ProxyUser = proxyUsername; | |||
ProxyPass = proxyPassword; | |||
ToThrow = new InvalidOperationException(); | |||
} | |||
/// <summary> | |||
/// Establishes a connection to a remote device. | |||
/// </summary> | |||
/// <param name="remoteEP">An EndPoint that represents the remote device.</param> | |||
/// <exception cref="ArgumentNullException">The remoteEP parameter is a null reference (Nothing in Visual Basic).</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="ProxyException">An error occured while talking to the proxy server.</exception> | |||
public new void Connect(EndPoint remoteEP) { | |||
if (remoteEP == null) | |||
throw new ArgumentNullException("<remoteEP> cannot be null."); | |||
if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) | |||
base.Connect(remoteEP); | |||
else { | |||
base.Connect(ProxyEndPoint); | |||
if (ProxyType == ProxyTypes.Socks4) | |||
(new Socks4Handler(this, ProxyUser)).Negotiate((IPEndPoint)remoteEP); | |||
else if (ProxyType == ProxyTypes.Socks5) | |||
(new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate((IPEndPoint)remoteEP); | |||
} | |||
} | |||
/// <summary> | |||
/// Establishes a connection to a remote device. | |||
/// </summary> | |||
/// <param name="host">The remote host to connect to.</param> | |||
/// <param name="port">The remote port to connect to.</param> | |||
/// <exception cref="ArgumentNullException">The host parameter is a null reference (Nothing in Visual Basic).</exception> | |||
/// <exception cref="ArgumentException">The port parameter is invalid.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="ProxyException">An error occured while talking to the proxy server.</exception> | |||
/// <remarks>If you use this method with a SOCKS4 server, it will let the server resolve the hostname. Not all SOCKS4 servers support this 'remote DNS' though.</remarks> | |||
public void Connect(string host, int port) { | |||
if (host == null) | |||
throw new ArgumentNullException("<host> cannot be null."); | |||
if (port <= 0 || port > 65535) | |||
throw new ArgumentException("Invalid port."); | |||
if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) | |||
base.Connect(new IPEndPoint(Dns.Resolve(host).AddressList[0], port)); | |||
else { | |||
base.Connect(ProxyEndPoint); | |||
if (ProxyType == ProxyTypes.Socks4) | |||
(new Socks4Handler(this, ProxyUser)).Negotiate(host, port); | |||
else if (ProxyType == ProxyTypes.Socks5) | |||
(new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate(host, port); | |||
} | |||
} | |||
/// <summary> | |||
/// Begins an asynchronous request for a connection to a network device. | |||
/// </summary> | |||
/// <param name="remoteEP">An EndPoint that represents the remote device.</param> | |||
/// <param name="callback">The AsyncCallback delegate.</param> | |||
/// <param name="state">An object that contains state information for this request.</param> | |||
/// <returns>An IAsyncResult that references the asynchronous connection.</returns> | |||
/// <exception cref="ArgumentNullException">The remoteEP parameter is a null reference (Nothing in Visual Basic).</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while creating the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public new IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, object state) { | |||
if (remoteEP == null || callback == null) | |||
throw new ArgumentNullException(); | |||
if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) { | |||
return base.BeginConnect(remoteEP, callback, state); | |||
} else { | |||
CallBack = callback; | |||
if (ProxyType == ProxyTypes.Socks4) { | |||
AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate((IPEndPoint)remoteEP, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); | |||
return AsyncResult; | |||
} else if(ProxyType == ProxyTypes.Socks5) { | |||
AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate((IPEndPoint)remoteEP, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); | |||
return AsyncResult; | |||
} | |||
return null; | |||
} | |||
} | |||
/// <summary> | |||
/// Begins an asynchronous request for a connection to a network device. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port on the remote host to connect to.</param> | |||
/// <param name="callback">The AsyncCallback delegate.</param> | |||
/// <param name="state">An object that contains state information for this request.</param> | |||
/// <returns>An IAsyncResult that references the asynchronous connection.</returns> | |||
/// <exception cref="ArgumentNullException">The host parameter is a null reference (Nothing in Visual Basic).</exception> | |||
/// <exception cref="ArgumentException">The port parameter is invalid.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while creating the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public IAsyncResult BeginConnect(string host, int port, AsyncCallback callback, object state) { | |||
if (host == null || callback == null) | |||
throw new ArgumentNullException(); | |||
if (port <= 0 || port > 65535) | |||
throw new ArgumentException(); | |||
CallBack = callback; | |||
if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) { | |||
RemotePort = port; | |||
AsyncResult = BeginDns(host, new HandShakeComplete(this.OnHandShakeComplete)); | |||
return AsyncResult; | |||
} else { | |||
if (ProxyType == ProxyTypes.Socks4) { | |||
AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate(host, port, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); | |||
return AsyncResult; | |||
} else if(ProxyType == ProxyTypes.Socks5) { | |||
AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate(host, port, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); | |||
return AsyncResult; | |||
} | |||
return null; | |||
} | |||
} | |||
/// <summary> | |||
/// Ends a pending asynchronous connection request. | |||
/// </summary> | |||
/// <param name="asyncResult">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
/// <exception cref="ArgumentNullException">The asyncResult parameter is a null reference (Nothing in Visual Basic).</exception> | |||
/// <exception cref="ArgumentException">The asyncResult parameter was not returned by a call to the BeginConnect method.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="InvalidOperationException">EndConnect was previously called for the asynchronous connection.</exception> | |||
/// <exception cref="ProxyException">The proxy server refused the connection.</exception> | |||
public new void EndConnect(IAsyncResult asyncResult) { | |||
if (asyncResult == null) | |||
throw new ArgumentNullException(); | |||
if (!asyncResult.IsCompleted) | |||
throw new ArgumentException(); | |||
if (ToThrow != null) | |||
throw ToThrow; | |||
return; | |||
} | |||
/// <summary> | |||
/// Begins an asynchronous request to resolve a DNS host name or IP address in dotted-quad notation to an IPAddress instance. | |||
/// </summary> | |||
/// <param name="host">The host to resolve.</param> | |||
/// <param name="callback">The method to call when the hostname has been resolved.</param> | |||
/// <returns>An IAsyncResult instance that references the asynchronous request.</returns> | |||
/// <exception cref="SocketException">There was an error while trying to resolve the host.</exception> | |||
internal IAsyncProxyResult BeginDns(string host, HandShakeComplete callback) { | |||
try { | |||
Dns.BeginResolve(host, new AsyncCallback(this.OnResolved), this); | |||
return new IAsyncProxyResult(); | |||
} catch { | |||
throw new SocketException(); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the specified hostname has been resolved. | |||
/// </summary> | |||
/// <param name="asyncResult">The result of the asynchronous operation.</param> | |||
private void OnResolved(IAsyncResult asyncResult) { | |||
try { | |||
IPHostEntry dns = Dns.EndResolve(asyncResult); | |||
base.BeginConnect(new IPEndPoint(dns.AddressList[0], RemotePort), new AsyncCallback(this.OnConnect), State); | |||
} catch (Exception e) { | |||
OnHandShakeComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the Socket is connected to the remote host. | |||
/// </summary> | |||
/// <param name="asyncResult">The result of the asynchronous operation.</param> | |||
private void OnConnect(IAsyncResult asyncResult) { | |||
try { | |||
base.EndConnect(asyncResult); | |||
OnHandShakeComplete(null); | |||
} catch (Exception e) { | |||
OnHandShakeComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the Socket has finished talking to the proxy server and is ready to relay data. | |||
/// </summary> | |||
/// <param name="error">The error to throw when the EndConnect method is called.</param> | |||
private void OnHandShakeComplete(Exception error) { | |||
if (error != null) | |||
this.Close(); | |||
ToThrow = error; | |||
AsyncResult.Reset(); | |||
if (CallBack != null) | |||
CallBack(AsyncResult); | |||
} | |||
/// <summary> | |||
/// Gets or sets the EndPoint of the proxy server. | |||
/// </summary> | |||
/// <value>An IPEndPoint object that holds the IP address and the port of the proxy server.</value> | |||
public IPEndPoint ProxyEndPoint { | |||
get { | |||
return m_ProxyEndPoint; | |||
} | |||
set { | |||
m_ProxyEndPoint = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the type of proxy server to use. | |||
/// </summary> | |||
/// <value>One of the ProxyTypes values.</value> | |||
public ProxyTypes ProxyType { | |||
get { | |||
return m_ProxyType; | |||
} | |||
set { | |||
m_ProxyType = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets a user-defined object. | |||
/// </summary> | |||
/// <value>The user-defined object.</value> | |||
private object State { | |||
get { | |||
return m_State; | |||
} | |||
set { | |||
m_State = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the username to use when authenticating with the proxy. | |||
/// </summary> | |||
/// <value>A string that holds the username that's used when authenticating with the proxy.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
public string ProxyUser { | |||
get { | |||
return m_ProxyUser; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_ProxyUser = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the password to use when authenticating with the proxy. | |||
/// </summary> | |||
/// <value>A string that holds the password that's used when authenticating with the proxy.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
public string ProxyPass { | |||
get { | |||
return m_ProxyPass; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_ProxyPass = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the asynchronous result object. | |||
/// </summary> | |||
/// <value>An instance of the IAsyncProxyResult class.</value> | |||
private IAsyncProxyResult AsyncResult { | |||
get { | |||
return m_AsyncResult; | |||
} | |||
set { | |||
m_AsyncResult = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the exception to throw when the EndConnect method is called. | |||
/// </summary> | |||
/// <value>An instance of the Exception class (or subclasses of Exception).</value> | |||
private Exception ToThrow { | |||
get { | |||
return m_ToThrow; | |||
} | |||
set { | |||
m_ToThrow = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the remote port the user wants to connect to. | |||
/// </summary> | |||
/// <value>An integer that specifies the port the user wants to connect to.</value> | |||
private int RemotePort { | |||
get { | |||
return m_RemotePort; | |||
} | |||
set { | |||
m_RemotePort = value; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Holds the value of the State property.</summary> | |||
private object m_State; | |||
/// <summary>Holds the value of the ProxyEndPoint property.</summary> | |||
private IPEndPoint m_ProxyEndPoint = null; | |||
/// <summary>Holds the value of the ProxyType property.</summary> | |||
private ProxyTypes m_ProxyType = ProxyTypes.None; | |||
/// <summary>Holds the value of the ProxyUser property.</summary> | |||
private string m_ProxyUser = null; | |||
/// <summary>Holds the value of the ProxyPass property.</summary> | |||
private string m_ProxyPass = null; | |||
/// <summary>Holds a pointer to the method that should be called when the Socket is connected to the remote device.</summary> | |||
private AsyncCallback CallBack = null; | |||
/// <summary>Holds the value of the AsyncResult property.</summary> | |||
private IAsyncProxyResult m_AsyncResult; | |||
/// <summary>Holds the value of the ToThrow property.</summary> | |||
private Exception m_ToThrow = null; | |||
/// <summary>Holds the value of the RemotePort property.</summary> | |||
private int m_RemotePort; | |||
} | |||
} |
@@ -1,234 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// Implements the SOCKS4[A] protocol. | |||
/// </summary> | |||
internal sealed class Socks4Handler : SocksHandler { | |||
/// <summary> | |||
/// Initilizes a new instance of the SocksHandler class. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <param name="user">The username to use when authenticating with the server.</param> | |||
/// <exception cref="ArgumentNullException"><c>server</c> -or- <c>user</c> is null.</exception> | |||
public Socks4Handler(Socket server, string user) : base(server, user) {} | |||
/// <summary> | |||
/// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port to connect to.</param> | |||
/// <returns>An array of bytes that has to be sent when the user wants to connect to a specific host/port combination.</returns> | |||
/// <remarks>Resolving the host name will be done at server side. Do note that some SOCKS4 servers do not implement this functionality.</remarks> | |||
/// <exception cref="ArgumentNullException"><c>host</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>port</c> is invalid.</exception> | |||
private byte[] GetHostPortBytes(string host, int port) { | |||
if (host == null) | |||
throw new ArgumentNullException(); | |||
if (port <= 0 || port > 65535) | |||
throw new ArgumentException(); | |||
byte [] connect = new byte[10 + Username.Length + host.Length]; | |||
connect[0] = 4; | |||
connect[1] = 1; | |||
Array.Copy(PortToBytes(port), 0, connect, 2, 2); | |||
connect[4] = connect[5] = connect[6] = 0; | |||
connect[7] = 1; | |||
Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); | |||
connect[8 + Username.Length] = 0; | |||
Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 9 + Username.Length, host.Length); | |||
connect[9 + Username.Length + host.Length] = 0; | |||
return connect; | |||
} | |||
/// <summary> | |||
/// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. | |||
/// </summary> | |||
/// <param name="remoteEP">The IPEndPoint to connect to.</param> | |||
/// <returns>An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint.</returns> | |||
/// <exception cref="ArgumentNullException"><c>remoteEP</c> is null.</exception> | |||
private byte[] GetEndPointBytes(IPEndPoint remoteEP) { | |||
if (remoteEP == null) | |||
throw new ArgumentNullException(); | |||
byte [] connect = new byte[9 + Username.Length]; | |||
connect[0] = 4; | |||
connect[1] = 1; | |||
Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 2, 2); | |||
Array.Copy(AddressToBytes(remoteEP.Address.Address), 0, connect, 4, 4); | |||
Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); | |||
connect[8 + Username.Length] = 0; | |||
return connect; | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port to connect to.</param> | |||
/// <exception cref="ArgumentNullException"><c>host</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>port</c> is invalid.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public override void Negotiate(string host, int port) { | |||
Negotiate(GetHostPortBytes(host, port)); | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="remoteEP">The IPEndPoint to connect to.</param> | |||
/// <exception cref="ArgumentNullException"><c>remoteEP</c> is null.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
public override void Negotiate(IPEndPoint remoteEP) { | |||
Negotiate(GetEndPointBytes(remoteEP)); | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="connect">The bytes to send when trying to authenticate.</param> | |||
/// <exception cref="ArgumentNullException"><c>connect</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>connect</c> is too small.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
private void Negotiate(byte [] connect) { | |||
if (connect == null) | |||
throw new ArgumentNullException(); | |||
if (connect.Length < 2) | |||
throw new ArgumentException(); | |||
Server.Send(connect); | |||
byte [] buffer = ReadBytes(8); | |||
if (buffer[1] != 90) { | |||
Server.Close(); | |||
throw new ProxyException("Negotiation failed."); | |||
} | |||
} | |||
/// <summary> | |||
/// Starts negotiating asynchronously with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="host">The remote server to connect to.</param> | |||
/// <param name="port">The remote port to connect to.</param> | |||
/// <param name="callback">The method to call when the connection has been established.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint) { | |||
ProtocolComplete = callback; | |||
Buffer = GetHostPortBytes(host, port); | |||
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); | |||
AsyncResult = new IAsyncProxyResult(); | |||
return AsyncResult; | |||
} | |||
/// <summary> | |||
/// Starts negotiating asynchronously with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="remoteEP">An IPEndPoint that represents the remote device.</param> | |||
/// <param name="callback">The method to call when the connection has been established.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint) { | |||
ProtocolComplete = callback; | |||
Buffer = GetEndPointBytes(remoteEP); | |||
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); | |||
AsyncResult = new IAsyncProxyResult(); | |||
return AsyncResult; | |||
} | |||
/// <summary> | |||
/// Called when the Socket is connected to the remote proxy server. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnConnect(IAsyncResult ar) { | |||
try { | |||
Server.EndConnect(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the Socket has sent the handshake data. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnSent(IAsyncResult ar) { | |||
try { | |||
if (Server.EndSend(ar) < Buffer.Length) { | |||
ProtocolComplete(new SocketException()); | |||
return; | |||
} | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Buffer = new byte[8]; | |||
Received = 0; | |||
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the Socket has received a reply from the remote proxy server. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnReceive(IAsyncResult ar) { | |||
try { | |||
int received = Server.EndReceive(ar); | |||
if (received <= 0) { | |||
ProtocolComplete(new SocketException()); | |||
return; | |||
} | |||
Received += received; | |||
if (Received == 8) { | |||
if (Buffer[1] == 90) | |||
ProtocolComplete(null); | |||
else { | |||
Server.Close(); | |||
ProtocolComplete(new ProxyException("Negotiation failed.")); | |||
} | |||
} else { | |||
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
} | |||
} | |||
@@ -1,418 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// Implements the SOCKS5 protocol. | |||
/// </summary> | |||
internal sealed class Socks5Handler : SocksHandler { | |||
/// <summary> | |||
/// Initiliazes a new Socks5Handler instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <exception cref="ArgumentNullException"><c>server</c> is null.</exception> | |||
public Socks5Handler(Socket server) : this(server, "") {} | |||
/// <summary> | |||
/// Initiliazes a new Socks5Handler instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <param name="user">The username to use.</param> | |||
/// <exception cref="ArgumentNullException"><c>server</c> -or- <c>user</c> is null.</exception> | |||
public Socks5Handler(Socket server, string user) : this(server, user, "") {} | |||
/// <summary> | |||
/// Initiliazes a new Socks5Handler instance. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <param name="user">The username to use.</param> | |||
/// <param name="pass">The password to use.</param> | |||
/// <exception cref="ArgumentNullException"><c>server</c> -or- <c>user</c> -or- <c>pass</c> is null.</exception> | |||
public Socks5Handler(Socket server, string user, string pass) : base(server, user) { | |||
Password = pass; | |||
} | |||
/// <summary> | |||
/// Starts the synchronous authentication process. | |||
/// </summary> | |||
/// <exception cref="ProxyException">Authentication with the proxy server failed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
private void Authenticate() { | |||
Server.Send(new byte [] {5, 2, 0, 2}); | |||
byte[] buffer = ReadBytes(2); | |||
if (buffer[1] == 255) | |||
throw new ProxyException("No authentication method accepted."); | |||
AuthMethod authenticate; | |||
switch (buffer[1]) { | |||
case 0: | |||
authenticate = new AuthNone(Server); | |||
break; | |||
case 2: | |||
authenticate = new AuthUserPass(Server, Username, Password); | |||
break; | |||
default: | |||
throw new ProtocolViolationException(); | |||
} | |||
authenticate.Authenticate(); | |||
} | |||
/// <summary> | |||
/// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port to connect to.</param> | |||
/// <returns>An array of bytes that has to be sent when the user wants to connect to a specific host/port combination.</returns> | |||
/// <exception cref="ArgumentNullException"><c>host</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>port</c> or <c>host</c> is invalid.</exception> | |||
private byte[] GetHostPortBytes(string host, int port) { | |||
if (host == null) | |||
throw new ArgumentNullException(); | |||
if (port <= 0 || port > 65535 || host.Length > 255) | |||
throw new ArgumentException(); | |||
byte [] connect = new byte[7 + host.Length]; | |||
connect[0] = 5; | |||
connect[1] = 1; | |||
connect[2] = 0; //reserved | |||
connect[3] = 3; | |||
connect[4] = (byte)host.Length; | |||
Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 5, host.Length); | |||
Array.Copy(PortToBytes(port), 0, connect, host.Length + 5, 2); | |||
return connect; | |||
} | |||
/// <summary> | |||
/// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. | |||
/// </summary> | |||
/// <param name="remoteEP">The IPEndPoint to connect to.</param> | |||
/// <returns>An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint.</returns> | |||
/// <exception cref="ArgumentNullException"><c>remoteEP</c> is null.</exception> | |||
private byte[] GetEndPointBytes(IPEndPoint remoteEP) { | |||
if (remoteEP == null) | |||
throw new ArgumentNullException(); | |||
byte [] connect = new byte[10]; | |||
connect[0] = 5; | |||
connect[1] = 1; | |||
connect[2] = 0; //reserved | |||
connect[3] = 1; | |||
Array.Copy(AddressToBytes(remoteEP.Address.Address), 0, connect, 4, 4); | |||
Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 8, 2); | |||
return connect; | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port to connect to.</param> | |||
/// <exception cref="ArgumentNullException"><c>host</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>port</c> is invalid.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
public override void Negotiate(string host, int port) { | |||
Negotiate(GetHostPortBytes(host, port)); | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="remoteEP">The IPEndPoint to connect to.</param> | |||
/// <exception cref="ArgumentNullException"><c>remoteEP</c> is null.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
public override void Negotiate(IPEndPoint remoteEP) { | |||
Negotiate(GetEndPointBytes(remoteEP)); | |||
} | |||
/// <summary> | |||
/// Starts negotiating with the SOCKS server. | |||
/// </summary> | |||
/// <param name="connect">The bytes to send when trying to authenticate.</param> | |||
/// <exception cref="ArgumentNullException"><c>connect</c> is null.</exception> | |||
/// <exception cref="ArgumentException"><c>connect</c> is too small.</exception> | |||
/// <exception cref="ProxyException">The proxy rejected the request.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
/// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception> | |||
private void Negotiate(byte[] connect) { | |||
Authenticate(); | |||
Server.Send(connect); | |||
byte[] buffer = ReadBytes(4); | |||
if (buffer[1] != 0) { | |||
Server.Close(); | |||
throw new ProxyException(buffer[1]); | |||
} | |||
switch(buffer[3]) { | |||
case 1: | |||
buffer = ReadBytes(6); //IPv4 address with port | |||
break; | |||
case 3: | |||
buffer = ReadBytes(1); | |||
buffer = ReadBytes(buffer[0] + 2); //domain name with port | |||
break; | |||
case 4: | |||
buffer = ReadBytes(18); //IPv6 address with port | |||
break; | |||
default: | |||
Server.Close(); | |||
throw new ProtocolViolationException(); | |||
} | |||
} | |||
/// <summary> | |||
/// Starts negotiating asynchronously with the SOCKS server. | |||
/// </summary> | |||
/// <param name="host">The host to connect to.</param> | |||
/// <param name="port">The port to connect to.</param> | |||
/// <param name="callback">The method to call when the negotiation is complete.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint) { | |||
ProtocolComplete = callback; | |||
HandShake = GetHostPortBytes(host, port); | |||
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); | |||
AsyncResult = new IAsyncProxyResult(); | |||
return AsyncResult; | |||
} | |||
/// <summary> | |||
/// Starts negotiating asynchronously with the SOCKS server. | |||
/// </summary> | |||
/// <param name="remoteEP">An IPEndPoint that represents the remote device.</param> | |||
/// <param name="callback">The method to call when the negotiation is complete.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint) { | |||
ProtocolComplete = callback; | |||
HandShake = GetEndPointBytes(remoteEP); | |||
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); | |||
AsyncResult = new IAsyncProxyResult(); | |||
return AsyncResult; | |||
} | |||
/// <summary> | |||
/// Called when the socket is connected to the remote server. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnConnect(IAsyncResult ar) { | |||
try { | |||
Server.EndConnect(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Server.BeginSend(new byte [] {5, 2, 0, 2}, 0, 4, SocketFlags.None, new AsyncCallback(this.OnAuthSent), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the authentication bytes have been sent. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnAuthSent(IAsyncResult ar) { | |||
try { | |||
Server.EndSend(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Buffer = new byte[1024]; | |||
Received = 0; | |||
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when an authentication reply has been received. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnAuthReceive(IAsyncResult ar) { | |||
try { | |||
Received += Server.EndReceive(ar); | |||
if (Received <= 0) | |||
throw new SocketException(); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
if (Received < 2) { | |||
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server); | |||
} else { | |||
AuthMethod authenticate; | |||
switch(Buffer[1]) { | |||
case 0: | |||
authenticate = new AuthNone(Server); | |||
break; | |||
case 2: | |||
authenticate = new AuthUserPass(Server, Username, Password); | |||
break; | |||
default: | |||
ProtocolComplete(new SocketException()); | |||
return; | |||
} | |||
authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated)); | |||
} | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the socket has been successfully authenticated with the server. | |||
/// </summary> | |||
/// <param name="e">The exception that has occured while authenticating, or <em>null</em> if no error occured.</param> | |||
private void OnAuthenticated(Exception e) { | |||
if (e != null) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); | |||
} catch (Exception ex) { | |||
ProtocolComplete(ex); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when the connection request has been sent. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnSent(IAsyncResult ar) { | |||
try { | |||
Server.EndSend(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
Buffer = new byte[5]; | |||
Received = 0; | |||
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Called when a connection reply has been received. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnReceive(IAsyncResult ar) { | |||
try { | |||
Received += Server.EndReceive(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
if (Received == Buffer.Length) | |||
ProcessReply(Buffer); | |||
else | |||
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Processes the received reply. | |||
/// </summary> | |||
/// <param name="buffer">The received reply</param> | |||
/// <exception cref="ProtocolViolationException">The received reply is invalid.</exception> | |||
private void ProcessReply(byte[] buffer) { | |||
switch(buffer[3]) { | |||
case 1: | |||
Buffer = new byte[5]; //IPv4 address with port - 1 byte | |||
break; | |||
case 3: | |||
Buffer = new byte[buffer[4] + 2]; //domain name with port | |||
break; | |||
case 4: | |||
buffer = new byte[17]; //IPv6 address with port - 1 byte | |||
break; | |||
default: | |||
throw new ProtocolViolationException(); | |||
} | |||
Received = 0; | |||
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); | |||
} | |||
/// <summary> | |||
/// Called when the last bytes are read from the socket. | |||
/// </summary> | |||
/// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param> | |||
private void OnReadLast(IAsyncResult ar) { | |||
try { | |||
Received += Server.EndReceive(ar); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
return; | |||
} | |||
try { | |||
if (Received == Buffer.Length) | |||
ProtocolComplete(null); | |||
else | |||
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); | |||
} catch (Exception e) { | |||
ProtocolComplete(e); | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the password to use when authenticating with the SOCKS5 server. | |||
/// </summary> | |||
/// <value>The password to use when authenticating with the SOCKS5 server.</value> | |||
private string Password { | |||
get { | |||
return m_Password; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Password = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the bytes to use when sending a connect request to the proxy server. | |||
/// </summary> | |||
/// <value>The array of bytes to use when sending a connect request to the proxy server.</value> | |||
private byte[] HandShake { | |||
get { | |||
return m_HandShake; | |||
} | |||
set { | |||
m_HandShake = value; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Holds the value of the Password property.</summary> | |||
private string m_Password; | |||
/// <summary>Holds the value of the HandShake property.</summary> | |||
private byte[] m_HandShake; | |||
} | |||
} |
@@ -1,204 +0,0 @@ | |||
/* | |||
Copyright ?2002, The KPD-Team | |||
All rights reserved. | |||
http://www.mentalis.org/ | |||
Redistribution and use in source and binary forms, with or without | |||
modification, are permitted provided that the following conditions | |||
are met: | |||
- Redistributions of source code must retain the above copyright | |||
notice, this list of conditions and the following disclaimer. | |||
- Neither the name of the KPD-Team, nor the names of its contributors | |||
may be used to endorse or promote products derived from this | |||
software without specific prior written permission. | |||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |||
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |||
OF THE POSSIBILITY OF SUCH DAMAGE. | |||
*/ | |||
using System; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
namespace Shadowsocks._3rd.ProxySocket { | |||
/// <summary> | |||
/// References the callback method to be called when the protocol negotiation is completed. | |||
/// </summary> | |||
internal delegate void HandShakeComplete(Exception error); | |||
/// <summary> | |||
/// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited. | |||
/// </summary> | |||
internal abstract class SocksHandler { | |||
/// <summary> | |||
/// Initilizes a new instance of the SocksHandler class. | |||
/// </summary> | |||
/// <param name="server">The socket connection with the proxy server.</param> | |||
/// <param name="user">The username to use when authenticating with the server.</param> | |||
/// <exception cref="ArgumentNullException"><c>server</c> -or- <c>user</c> is null.</exception> | |||
public SocksHandler(Socket server, string user) { | |||
Server = server; | |||
Username = user; | |||
} | |||
/// <summary> | |||
/// Converts a port number to an array of bytes. | |||
/// </summary> | |||
/// <param name="port">The port to convert.</param> | |||
/// <returns>An array of two bytes that represents the specified port.</returns> | |||
protected byte[] PortToBytes(int port) { | |||
byte [] ret = new byte[2]; | |||
ret[0] = (byte)(port / 256); | |||
ret[1] = (byte)(port % 256); | |||
return ret; | |||
} | |||
/// <summary> | |||
/// Converts an IP address to an array of bytes. | |||
/// </summary> | |||
/// <param name="address">The IP address to convert.</param> | |||
/// <returns>An array of four bytes that represents the specified IP address.</returns> | |||
protected byte[] AddressToBytes(long address) { | |||
byte [] ret = new byte[4]; | |||
ret[0] = (byte)(address % 256); | |||
ret[1] = (byte)((address / 256) % 256); | |||
ret[2] = (byte)((address / 65536) % 256); | |||
ret[3] = (byte)(address / 16777216); | |||
return ret; | |||
} | |||
/// <summary> | |||
/// Reads a specified number of bytes from the Server socket. | |||
/// </summary> | |||
/// <param name="count">The number of bytes to return.</param> | |||
/// <returns>An array of bytes.</returns> | |||
/// <exception cref="ArgumentException">The number of bytes to read is invalid.</exception> | |||
/// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception> | |||
/// <exception cref="ObjectDisposedException">The Socket has been closed.</exception> | |||
protected byte[] ReadBytes(int count) { | |||
if (count <= 0) | |||
throw new ArgumentException(); | |||
byte[] buffer = new byte[count]; | |||
int received = 0; | |||
while(received != count) { | |||
received += Server.Receive(buffer, received, count - received, SocketFlags.None); | |||
} | |||
return buffer; | |||
} | |||
/// <summary> | |||
/// Gets or sets the socket connection with the proxy server. | |||
/// </summary> | |||
/// <value>A Socket object that represents the connection with the proxy server.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
protected Socket Server { | |||
get { | |||
return m_Server; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Server = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the username to use when authenticating with the proxy server. | |||
/// </summary> | |||
/// <value>A string that holds the username to use when authenticating with the proxy server.</value> | |||
/// <exception cref="ArgumentNullException">The specified value is null.</exception> | |||
protected string Username { | |||
get { | |||
return m_Username; | |||
} | |||
set { | |||
if (value == null) | |||
throw new ArgumentNullException(); | |||
m_Username = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the return value of the BeginConnect call. | |||
/// </summary> | |||
/// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value> | |||
protected IAsyncProxyResult AsyncResult { | |||
get { | |||
return m_AsyncResult; | |||
} | |||
set { | |||
m_AsyncResult = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets a byte buffer. | |||
/// </summary> | |||
/// <value>An array of bytes.</value> | |||
protected byte[] Buffer { | |||
get { | |||
return m_Buffer; | |||
} | |||
set { | |||
m_Buffer = value; | |||
} | |||
} | |||
/// <summary> | |||
/// Gets or sets the number of bytes that have been received from the remote proxy server. | |||
/// </summary> | |||
/// <value>An integer that holds the number of bytes that have been received from the remote proxy server.</value> | |||
protected int Received { | |||
get { | |||
return m_Received; | |||
} | |||
set { | |||
m_Received = value; | |||
} | |||
} | |||
// private variables | |||
/// <summary>Holds the value of the Server property.</summary> | |||
private Socket m_Server; | |||
/// <summary>Holds the value of the Username property.</summary> | |||
private string m_Username; | |||
/// <summary>Holds the value of the AsyncResult property.</summary> | |||
private IAsyncProxyResult m_AsyncResult; | |||
/// <summary>Holds the value of the Buffer property.</summary> | |||
private byte[] m_Buffer; | |||
/// <summary>Holds the value of the Received property.</summary> | |||
private int m_Received; | |||
/// <summary>Holds the address of the method to call when the SOCKS protocol has been completed.</summary> | |||
protected HandShakeComplete ProtocolComplete; | |||
/// <summary> | |||
/// Starts negotiating with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="host">The remote server to connect to.</param> | |||
/// <param name="port">The remote port to connect to.</param> | |||
public abstract void Negotiate(string host, int port); | |||
/// <summary> | |||
/// Starts negotiating with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="remoteEP">The remote endpoint to connect to.</param> | |||
public abstract void Negotiate(IPEndPoint remoteEP); | |||
/// <summary> | |||
/// Starts negotiating asynchronously with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="remoteEP">An IPEndPoint that represents the remote device. </param> | |||
/// <param name="callback">The method to call when the connection has been established.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint); | |||
/// <summary> | |||
/// Starts negotiating asynchronously with a SOCKS proxy server. | |||
/// </summary> | |||
/// <param name="host">The remote server to connect to.</param> | |||
/// <param name="port">The remote port to connect to.</param> | |||
/// <param name="callback">The method to call when the connection has been established.</param> | |||
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> | |||
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> | |||
public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint); | |||
} | |||
} |
@@ -1,283 +0,0 @@ | |||
using System; | |||
using System.Collections.Specialized; | |||
using System.IO; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks._3rd.ProxySocket | |||
{ | |||
public class SocksHttpWebRequest : WebRequest | |||
{ | |||
#region Private | |||
private Encoding _correctEncoding; | |||
#endregion Private | |||
#region Member Variables | |||
private readonly Uri _requestUri; | |||
private WebHeaderCollection _requestHeaders; | |||
private string _method; | |||
private SocksHttpWebResponse _response; | |||
private string _requestMessage; | |||
private byte[] _requestContentBuffer; | |||
// darn MS for making everything internal (yeah, I'm talking about you, System.net.KnownHttpVerb) | |||
static readonly StringCollection validHttpVerbs = | |||
new StringCollection { "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS" }; | |||
#endregion | |||
#region Constructor | |||
private SocksHttpWebRequest(Uri requestUri) | |||
{ | |||
_requestUri = requestUri; | |||
_correctEncoding = Encoding.Default; | |||
} | |||
#endregion | |||
#region WebRequest Members | |||
public override WebResponse GetResponse() | |||
{ | |||
if (Proxy == null) | |||
{ | |||
throw new InvalidOperationException("Proxy property cannot be null."); | |||
} | |||
if (String.IsNullOrEmpty(Method)) | |||
{ | |||
throw new InvalidOperationException("Method has not been set."); | |||
} | |||
if (RequestSubmitted) | |||
{ | |||
return _response; | |||
} | |||
_response = InternalGetResponse(); | |||
RequestSubmitted = true; | |||
return _response; | |||
} | |||
public override Uri RequestUri | |||
{ | |||
get { return _requestUri; } | |||
} | |||
public override IWebProxy Proxy { get; set; } | |||
public override WebHeaderCollection Headers | |||
{ | |||
get | |||
{ | |||
if (_requestHeaders == null) | |||
{ | |||
_requestHeaders = new WebHeaderCollection(); | |||
} | |||
return _requestHeaders; | |||
} | |||
set | |||
{ | |||
if (RequestSubmitted) | |||
{ | |||
throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); | |||
} | |||
_requestHeaders = value; | |||
} | |||
} | |||
public bool RequestSubmitted { get; private set; } | |||
public override string Method | |||
{ | |||
get | |||
{ | |||
return _method ?? "GET"; | |||
} | |||
set | |||
{ | |||
if (validHttpVerbs.Contains(value)) | |||
{ | |||
_method = value; | |||
} | |||
else | |||
{ | |||
throw new ArgumentOutOfRangeException("value", string.Format("'{0}' is not a known HTTP verb.", value)); | |||
} | |||
} | |||
} | |||
public override long ContentLength { get; set; } | |||
public override string ContentType { get; set; } | |||
public override Stream GetRequestStream() | |||
{ | |||
if (RequestSubmitted) | |||
{ | |||
throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); | |||
} | |||
if (_requestContentBuffer == null) | |||
{ | |||
_requestContentBuffer = new byte[ContentLength]; | |||
} | |||
else if (ContentLength == default(long)) | |||
{ | |||
_requestContentBuffer = new byte[int.MaxValue]; | |||
} | |||
else if (_requestContentBuffer.Length != ContentLength) | |||
{ | |||
Array.Resize(ref _requestContentBuffer, (int)ContentLength); | |||
} | |||
return new MemoryStream(_requestContentBuffer); | |||
} | |||
#endregion | |||
#region Methods | |||
public static new WebRequest Create(string requestUri) | |||
{ | |||
return new SocksHttpWebRequest(new Uri(requestUri)); | |||
} | |||
public static new WebRequest Create(Uri requestUri) | |||
{ | |||
return new SocksHttpWebRequest(requestUri); | |||
} | |||
private string BuildHttpRequestMessage() | |||
{ | |||
if (RequestSubmitted) | |||
{ | |||
throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); | |||
} | |||
var message = new StringBuilder(); | |||
message.AppendFormat("{0} {1} HTTP/1.0\r\nHost: {2}\r\n", Method, RequestUri.PathAndQuery, RequestUri.Host); | |||
// add the headers | |||
foreach (var key in Headers.Keys) | |||
{ | |||
message.AppendFormat("{0}: {1}\r\n", key, Headers[key.ToString()]); | |||
} | |||
if (!string.IsNullOrEmpty(ContentType)) | |||
{ | |||
message.AppendFormat("Content-Type: {0}\r\n", ContentType); | |||
} | |||
if (ContentLength > 0) | |||
{ | |||
message.AppendFormat("Content-Length: {0}\r\n", ContentLength); | |||
} | |||
// add a blank line to indicate the end of the headers | |||
message.Append("\r\n"); | |||
// add content | |||
if (_requestContentBuffer != null && _requestContentBuffer.Length > 0) | |||
{ | |||
using (var stream = new MemoryStream(_requestContentBuffer, false)) | |||
{ | |||
using (var reader = new StreamReader(stream)) | |||
{ | |||
message.Append(reader.ReadToEnd()); | |||
} | |||
} | |||
} | |||
return message.ToString(); | |||
} | |||
private SocksHttpWebResponse InternalGetResponse() | |||
{ | |||
var response = new StringBuilder(); | |||
using (var _socksConnection = | |||
new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | |||
{ | |||
var proxyUri = Proxy.GetProxy(RequestUri); | |||
var ipAddress = GetProxyIpAddress(proxyUri); | |||
_socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port); | |||
_socksConnection.ProxyType = ProxyTypes.Socks5; | |||
// open connection | |||
_socksConnection.Connect(RequestUri.Host, 80); | |||
// send an HTTP request | |||
_socksConnection.Send(_correctEncoding.GetBytes(RequestMessage)); | |||
// read the HTTP reply | |||
var buffer = new byte[1024]; | |||
var bytesReceived = _socksConnection.Receive(buffer); | |||
while (bytesReceived > 0) | |||
{ | |||
string chunk = _correctEncoding.GetString(buffer, 0, bytesReceived); | |||
string encString = EncodingHelper.GetEncodingFromChunk(chunk); | |||
if (!string.IsNullOrEmpty(encString)) | |||
{ | |||
try | |||
{ | |||
_correctEncoding = Encoding.GetEncoding(encString); | |||
} | |||
catch | |||
{ | |||
//TODO: do something here | |||
} | |||
} | |||
response.Append(chunk); | |||
bytesReceived = _socksConnection.Receive(buffer); | |||
} | |||
} | |||
return new SocksHttpWebResponse(response.ToString(),_correctEncoding); | |||
} | |||
private static IPAddress GetProxyIpAddress(Uri proxyUri) | |||
{ | |||
IPAddress ipAddress; | |||
if (!IPAddress.TryParse(proxyUri.Host, out ipAddress)) | |||
{ | |||
try | |||
{ | |||
return Dns.GetHostEntry(proxyUri.Host).AddressList[0]; | |||
} | |||
catch (Exception e) | |||
{ | |||
throw new InvalidOperationException( | |||
string.Format("Unable to resolve proxy hostname '{0}' to a valid IP address.", proxyUri.Host), e); | |||
} | |||
} | |||
return ipAddress; | |||
} | |||
#endregion | |||
#region Properties | |||
public Encoding CorrectEncoding | |||
{ | |||
get | |||
{ | |||
return _correctEncoding; | |||
} | |||
} | |||
public string RequestMessage | |||
{ | |||
get | |||
{ | |||
if (string.IsNullOrEmpty(_requestMessage)) | |||
{ | |||
_requestMessage = BuildHttpRequestMessage(); | |||
} | |||
return _requestMessage; | |||
} | |||
} | |||
#endregion | |||
} | |||
} |
@@ -1,109 +0,0 @@ | |||
using System; | |||
using System.IO; | |||
using System.Net; | |||
using System.Text; | |||
namespace Shadowsocks._3rd.ProxySocket | |||
{ | |||
public class SocksHttpWebResponse : WebResponse | |||
{ | |||
#region Member Variables | |||
private WebHeaderCollection _httpResponseHeaders; | |||
private string _responseContent; | |||
#endregion | |||
#region Constructors | |||
public SocksHttpWebResponse(string httpResponseMessage) | |||
{ | |||
SetHeadersAndResponseContent(httpResponseMessage); | |||
CorrectEncoding = Encoding.Default; | |||
} | |||
public SocksHttpWebResponse(string httpResponseMessage, Encoding encoding) | |||
{ | |||
SetHeadersAndResponseContent(httpResponseMessage); | |||
CorrectEncoding = encoding; | |||
} | |||
#endregion | |||
#region WebResponse Members | |||
public override Stream GetResponseStream() | |||
{ | |||
return ResponseContent.Length == 0 ? Stream.Null : new MemoryStream(CorrectEncoding.GetBytes(ResponseContent)); | |||
} | |||
public override void Close() { /* the base implementation throws an exception */ } | |||
public override WebHeaderCollection Headers | |||
{ | |||
get | |||
{ | |||
if (_httpResponseHeaders == null) | |||
{ | |||
_httpResponseHeaders = new WebHeaderCollection(); | |||
} | |||
return _httpResponseHeaders; | |||
} | |||
} | |||
public override long ContentLength | |||
{ | |||
get | |||
{ | |||
return ResponseContent.Length; | |||
} | |||
set | |||
{ | |||
throw new NotSupportedException(); | |||
} | |||
} | |||
#endregion | |||
#region Methods | |||
private void SetHeadersAndResponseContent(string responseMessage) | |||
{ | |||
if (string.IsNullOrEmpty(responseMessage)) | |||
return; | |||
// the HTTP headers can be found before the first blank line | |||
var indexOfFirstBlankLine = responseMessage.IndexOf("\r\n\r\n"); | |||
var headers = responseMessage.Substring(0, indexOfFirstBlankLine); | |||
var headerValues = headers.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); | |||
// ignore the first line in the header since it is the HTTP response code | |||
for (int i = 1; i < headerValues.Length; i++) | |||
{ | |||
var headerEntry = headerValues[i].Split(new[] { ':' }); | |||
Headers.Add(headerEntry[0], headerEntry[1]); | |||
} | |||
ResponseContent = responseMessage.Substring(indexOfFirstBlankLine + 4); | |||
} | |||
#endregion | |||
#region Properties | |||
private string ResponseContent | |||
{ | |||
get { return _responseContent ?? string.Empty; } | |||
set { _responseContent = value; } | |||
} | |||
public Encoding CorrectEncoding | |||
{ | |||
get;set; | |||
} | |||
#endregion | |||
} | |||
} |
@@ -1,312 +0,0 @@ | |||
using System; | |||
using System.Net; | |||
namespace Shadowsocks._3rd.ProxySocket | |||
{ | |||
#region old | |||
/* | |||
public class Calculagraph | |||
{ | |||
/// <summary> | |||
/// 时间到事件 | |||
/// </summary> | |||
public event TimeoutCaller TimeOver; | |||
/// <summary> | |||
/// 开始时间 | |||
/// </summary> | |||
private DateTime _startTime; | |||
private TimeSpan _timeout = new TimeSpan(0, 0, 10); | |||
private bool _hasStarted = false; | |||
object _userdata; | |||
/// <summary> | |||
/// 计时器构造方法 | |||
/// </summary> | |||
/// <param name="userdata">计时结束时回调的用户数据</param> | |||
public Calculagraph(object userdata) | |||
{ | |||
TimeOver += new TimeoutCaller(OnTimeOver); | |||
_userdata = userdata; | |||
} | |||
/// <summary> | |||
/// 超时退出 | |||
/// </summary> | |||
/// <param name="userdata"></param> | |||
public virtual void OnTimeOver(object userdata) | |||
{ | |||
Stop(); | |||
} | |||
/// <summary> | |||
/// 过期时间(秒) | |||
/// </summary> | |||
public int Timeout | |||
{ | |||
get | |||
{ | |||
return _timeout.Seconds; | |||
} | |||
set | |||
{ | |||
if (value <= 0) | |||
return; | |||
_timeout = new TimeSpan(0, 0, value); | |||
} | |||
} | |||
/// <summary> | |||
/// 是否已经开始计时 | |||
/// </summary> | |||
public bool HasStarted | |||
{ | |||
get | |||
{ | |||
return _hasStarted; | |||
} | |||
} | |||
/// <summary> | |||
/// 开始计时 | |||
/// </summary> | |||
public void Start() | |||
{ | |||
Reset(); | |||
_hasStarted = true; | |||
System.Threading.Thread th = new System.Threading.Thread(WaitCall); | |||
th.IsBackground = true; | |||
th.Start(); | |||
} | |||
/// <summary> | |||
/// 重置 | |||
/// </summary> | |||
public void Reset() | |||
{ | |||
_startTime = DateTime.Now; | |||
} | |||
/// <summary> | |||
/// 停止计时 | |||
/// </summary> | |||
public void Stop() | |||
{ | |||
_hasStarted = false; | |||
} | |||
/// <summary> | |||
/// 检查是否过期 | |||
/// </summary> | |||
/// <returns></returns> | |||
private bool checkTimeout() | |||
{ | |||
return (DateTime.Now - _startTime).Seconds >= Timeout; | |||
} | |||
private void WaitCall() | |||
{ | |||
try | |||
{ | |||
//循环检测是否过期 | |||
while (_hasStarted && !checkTimeout()) | |||
{ | |||
System.Threading.Thread.Sleep(1000); | |||
} | |||
if (TimeOver != null) | |||
TimeOver(_userdata); | |||
} | |||
catch (Exception) | |||
{ | |||
Stop(); | |||
} | |||
} | |||
} | |||
/// <summary> | |||
/// 过期时回调委托 | |||
/// </summary> | |||
/// <param name="userdata"></param> | |||
public delegate void TimeoutCaller(object userdata); | |||
public class CNNWebClient : WebClient | |||
{ | |||
private Calculagraph _timer; | |||
private int _timeOut = 5; | |||
/// <summary> | |||
/// 过期时间 | |||
/// </summary> | |||
public int Timeout | |||
{ | |||
get | |||
{ | |||
return _timeOut; | |||
} | |||
set | |||
{ | |||
if (value <= 0) | |||
_timeOut = 5; | |||
_timeOut = value; | |||
} | |||
} | |||
/// <summary> | |||
/// 重写GetWebRequest,添加WebRequest对象超时时间 | |||
/// </summary> | |||
/// <param name="address"></param> | |||
/// <returns></returns> | |||
protected override WebRequest GetWebRequest(Uri address) | |||
{ | |||
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); | |||
request.Timeout = 1000 * Timeout; | |||
request.ReadWriteTimeout = 1000 * Timeout; | |||
return request; | |||
} | |||
/// <summary> | |||
/// 带过期计时的下载 | |||
/// </summary> | |||
public void DownloadFileAsyncWithTimeout(Uri address, string fileName, object userToken) | |||
{ | |||
if (_timer == null) | |||
{ | |||
_timer = new Calculagraph(this); | |||
_timer.Timeout = Timeout; | |||
_timer.TimeOver += new TimeoutCaller(_timer_TimeOver); | |||
this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(CNNWebClient_DownloadProgressChanged); | |||
} | |||
DownloadFileAsync(address, fileName, userToken); | |||
_timer.Start(); | |||
} | |||
/// <summary> | |||
/// WebClient下载过程事件,接收到数据时引发 | |||
/// </summary> | |||
/// <param name="sender"></param> | |||
/// <param name="e"></param> | |||
void CNNWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) | |||
{ | |||
_timer.Reset();//重置计时器 | |||
} | |||
/// <summary> | |||
/// 计时器过期 | |||
/// </summary> | |||
/// <param name="userdata"></param> | |||
void _timer_TimeOver(object userdata) | |||
{ | |||
this.CancelAsync();//取消下载 | |||
} | |||
} | |||
*/ | |||
#endregion | |||
public class SocksWebClient : WebClient | |||
{ | |||
public IProxyDetails ProxyDetails { get; set; } | |||
public string UserAgent { get; set; } | |||
protected override WebRequest GetWebRequest(Uri address) | |||
{ | |||
WebRequest result = null; | |||
if (ProxyDetails != null) | |||
{ | |||
if (ProxyDetails.ProxyType == ProxyType.Proxy) | |||
{ | |||
result = (HttpWebRequest)WebRequest.Create(address); | |||
result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress); | |||
if (!string.IsNullOrEmpty(UserAgent)) | |||
((HttpWebRequest)result).UserAgent = UserAgent; | |||
} | |||
else if (ProxyDetails.ProxyType == ProxyType.Socks) | |||
{ | |||
result = SocksHttpWebRequest.Create(address); | |||
result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress); | |||
//TODO: implement user and password | |||
} | |||
else if (ProxyDetails.ProxyType == ProxyType.None) | |||
{ | |||
result = (HttpWebRequest)WebRequest.Create(address); | |||
if (!string.IsNullOrEmpty(UserAgent)) | |||
((HttpWebRequest)result).UserAgent = UserAgent; | |||
} | |||
} | |||
else | |||
{ | |||
result = (HttpWebRequest)WebRequest.Create(address); | |||
if (!string.IsNullOrEmpty(UserAgent)) | |||
((HttpWebRequest)result).UserAgent = UserAgent; | |||
} | |||
return result; | |||
} | |||
} | |||
public interface IProxyDetails | |||
{ | |||
ProxyType ProxyType { get; set; } | |||
/// <summary> | |||
/// adress and port | |||
/// </summary> | |||
string FullProxyAddress { get; set; } | |||
string ProxyAddress { get; set; } | |||
int ProxyPort { get; set; } | |||
string ProxyUserName { get; set; } | |||
string ProxyPassword { get; set; } | |||
} | |||
public class ProxyDetails : IProxyDetails | |||
{ | |||
public ProxyType ProxyType { get; set; } | |||
/// <summary> | |||
/// adress and port | |||
/// </summary> | |||
public string FullProxyAddress { get; set; } | |||
public string ProxyAddress { get; set; } | |||
public int ProxyPort { get; set; } | |||
public string ProxyUserName { get; set; } | |||
public string ProxyPassword { get; set; } | |||
public ProxyDetails() | |||
{ | |||
} | |||
public ProxyDetails(int port) | |||
{ | |||
FullProxyAddress = IPAddress.Loopback + ":" + port; | |||
ProxyAddress = IPAddress.Loopback.ToString(); | |||
ProxyPort = port; | |||
ProxyType = ProxyType.Socks; | |||
} | |||
} | |||
public enum ProxyType | |||
{ | |||
None = 0, | |||
Proxy = 1, | |||
Socks = 2 | |||
} | |||
public static class EncodingHelper | |||
{ | |||
public static string GetEncodingFromChunk(string chunk) | |||
{ | |||
string charset = null; | |||
int charsetStart = chunk.IndexOf("charset="); | |||
int charsetEnd = -1; | |||
if (charsetStart != -1) | |||
{ | |||
charsetEnd = chunk.IndexOfAny(new[] { ' ', '\"', ';', '\r', '\n' }, charsetStart); | |||
if (charsetEnd != -1) | |||
{ | |||
int start = charsetStart + 8; | |||
charset = chunk.Substring(start, charsetEnd - start + 1); | |||
charset = charset.TrimEnd(new Char[] { '>', '"', '\r', '\n' }); | |||
} | |||
} | |||
return charset; | |||
} | |||
} | |||
} |
@@ -1,754 +0,0 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Net; | |||
using System.Text; | |||
namespace Shadowsocks._3rd | |||
{ | |||
///<summary> | |||
/// QQWry 的摘要说明。 | |||
///</summary> | |||
public class QQWry | |||
{ | |||
#region Properties | |||
///<summary> | |||
///第一种模式 | |||
///</summary> | |||
private const byte REDIRECT_MODE_1 = 0x01; | |||
///<summary> | |||
///第二种模式 | |||
///</summary> | |||
private const byte REDIRECT_MODE_2 = 0x02; | |||
///<summary> | |||
///每条记录长度 | |||
///</summary> | |||
private const int IP_RECORD_LENGTH = 7; | |||
///<summary> | |||
///文件对象 | |||
///</summary> | |||
private FileStream ipFile; | |||
private const string unCountry = "未知国家"; | |||
private const string unArea = "未知地区"; | |||
///<summary> | |||
///索引开始位置 | |||
///</summary> | |||
private long ipBegin; | |||
///<summary> | |||
///索引结束位置 | |||
///</summary> | |||
private long ipEnd; | |||
///<summary> | |||
/// IP对象 | |||
///</summary> | |||
private IPLocation loc; | |||
///<summary> | |||
///存储文本内容 | |||
///</summary> | |||
private byte[] buf; | |||
///<summary> | |||
///存储3字节 | |||
///</summary> | |||
private byte[] b3; | |||
///<summary> | |||
///存储4字节IP地址 | |||
///</summary> | |||
private byte[] b4; | |||
#endregion | |||
#region 构造函数 | |||
///<summary> | |||
///构造函数 | |||
///</summary> | |||
///<param name="ipfile">IP数据库文件绝对路径</param> | |||
public QQWry(string ipfile) | |||
{ | |||
buf = new byte[100]; | |||
b3 = new byte[3]; | |||
b4 = new byte[4]; | |||
try | |||
{ | |||
ipFile = new FileStream(ipfile, FileMode.Open); | |||
} | |||
catch (Exception ex) | |||
{ | |||
throw new Exception(ex.Message); | |||
} | |||
ipBegin = readLong4(0); | |||
ipEnd = readLong4(4); | |||
loc = new IPLocation(); | |||
} | |||
#endregion | |||
#region 根据IP地址搜索 | |||
///<summary> | |||
///搜索IP地址搜索 | |||
///</summary> | |||
///<param name="ip"></param> | |||
///<returns></returns> | |||
public IPLocation SearchIPLocation(string ip) | |||
{ | |||
//将字符IP转换为字节 | |||
string[] ipSp = ip.Split('.'); | |||
if (ipSp.Length != 4) | |||
{ | |||
throw new ArgumentOutOfRangeException("不是合法的IP地址!"); | |||
} | |||
byte[] IP = new byte[4]; | |||
for (int i = 0; i < IP.Length; i++) | |||
{ | |||
IP[i] = (byte) (Int32.Parse(ipSp[i]) & 0xFF); | |||
} | |||
IPLocation local = null; | |||
long offset = locateIP(IP); | |||
if (offset != -1) | |||
{ | |||
local = getIPLocation(offset); | |||
} | |||
if (local == null) | |||
{ | |||
local = new IPLocation(); | |||
local.area = unArea; | |||
local.country = unCountry; | |||
} | |||
return local; | |||
} | |||
#endregion | |||
#region 取得具体信息 | |||
///<summary> | |||
///取得具体信息 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<returns></returns> | |||
private IPLocation getIPLocation(long offset) | |||
{ | |||
ipFile.Position = offset + 4; | |||
//读取第一个字节判断是否是标志字节 | |||
byte one = (byte) ipFile.ReadByte(); | |||
if (one == REDIRECT_MODE_1) | |||
{ | |||
//第一种模式 | |||
//读取国家偏移 | |||
long countryOffset = readLong3(); | |||
//转至偏移处 | |||
ipFile.Position = countryOffset; | |||
//再次检查标志字节 | |||
byte b = (byte) ipFile.ReadByte(); | |||
if (b == REDIRECT_MODE_2) | |||
{ | |||
loc.country = readString(readLong3()); | |||
ipFile.Position = countryOffset + 4; | |||
} | |||
else | |||
loc.country = readString(countryOffset); | |||
//读取地区标志 | |||
loc.area = readArea(ipFile.Position); | |||
} | |||
else if (one == REDIRECT_MODE_2) | |||
{ | |||
//第二种模式 | |||
loc.country = readString(readLong3()); | |||
loc.area = readArea(offset + 8); | |||
} | |||
else | |||
{ | |||
//普通模式 | |||
loc.country = readString(--ipFile.Position); | |||
loc.area = readString(ipFile.Position); | |||
} | |||
return loc; | |||
} | |||
#endregion | |||
#region 取得地区信息 | |||
///<summary> | |||
///读取地区名称 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<returns></returns> | |||
private string readArea(long offset) | |||
{ | |||
ipFile.Position = offset; | |||
byte one = (byte) ipFile.ReadByte(); | |||
if (one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2) | |||
{ | |||
long areaOffset = readLong3(offset + 1); | |||
if (areaOffset == 0) | |||
return unArea; | |||
else | |||
{ | |||
return readString(areaOffset); | |||
} | |||
} | |||
else | |||
{ | |||
return readString(offset); | |||
} | |||
} | |||
#endregion | |||
#region 读取字符串 | |||
///<summary> | |||
///读取字符串 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<returns></returns> | |||
private string readString(long offset) | |||
{ | |||
ipFile.Position = offset; | |||
int i = 0; | |||
for (i = 0, buf[i] = (byte) ipFile.ReadByte(); buf[i] != (byte) (0); buf[++i] = (byte) ipFile.ReadByte()) ; | |||
if (i > 0) | |||
return Encoding.Default.GetString(buf, 0, i); | |||
else | |||
return ""; | |||
} | |||
#endregion | |||
#region 查找IP地址所在的绝对偏移量 | |||
/**/ | |||
///<summary> | |||
///查找IP地址所在的绝对偏移量 | |||
///</summary> | |||
///<param name="ip"></param> | |||
///<returns></returns> | |||
private long locateIP(byte[] ip) | |||
{ | |||
long m = 0; | |||
int r; | |||
//比较第一个IP项 | |||
readIP(ipBegin, b4); | |||
r = compareIP(ip, b4); | |||
if (r == 0) | |||
return ipBegin; | |||
else if (r < 0) | |||
return -1; | |||
//开始二分搜索 | |||
for (long i = ipBegin, j = ipEnd; i < j;) | |||
{ | |||
m = this.getMiddleOffset(i, j); | |||
readIP(m, b4); | |||
r = compareIP(ip, b4); | |||
if (r > 0) | |||
i = m; | |||
else if (r < 0) | |||
{ | |||
if (m == j) | |||
{ | |||
j -= IP_RECORD_LENGTH; | |||
m = j; | |||
} | |||
else | |||
{ | |||
j = m; | |||
} | |||
} | |||
else | |||
return readLong3(m + 4); | |||
} | |||
m = readLong3(m + 4); | |||
readIP(m, b4); | |||
r = compareIP(ip, b4); | |||
if (r <= 0) | |||
return m; | |||
else | |||
return -1; | |||
} | |||
#endregion | |||
#region 读出4字节的IP地址 | |||
/**/ | |||
///<summary> | |||
///从当前位置读取四字节,此四字节是IP地址 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<param name="ip"></param> | |||
private void readIP(long offset, byte[] ip) | |||
{ | |||
ipFile.Position = offset; | |||
ipFile.Read(ip, 0, ip.Length); | |||
byte tmp = ip[0]; | |||
ip[0] = ip[3]; | |||
ip[3] = tmp; | |||
tmp = ip[1]; | |||
ip[1] = ip[2]; | |||
ip[2] = tmp; | |||
} | |||
#endregion | |||
#region 比较IP地址是否相同 | |||
/**/ | |||
///<summary> | |||
///比较IP地址是否相同 | |||
///</summary> | |||
///<param name="ip"></param> | |||
///<param name="beginIP"></param> | |||
///<returns>0:相等,1:ip大于beginIP,-1:小于</returns> | |||
private int compareIP(byte[] ip, byte[] beginIP) | |||
{ | |||
for (int i = 0; i < 4; i++) | |||
{ | |||
int r = compareByte(ip[i], beginIP[i]); | |||
if (r != 0) | |||
return r; | |||
} | |||
return 0; | |||
} | |||
#endregion | |||
#region 比较两个字节是否相等 | |||
/**/ | |||
///<summary> | |||
///比较两个字节是否相等 | |||
///</summary> | |||
///<param name="bsrc"></param> | |||
///<param name="bdst"></param> | |||
///<returns></returns> | |||
private int compareByte(byte bsrc, byte bdst) | |||
{ | |||
if ((bsrc & 0xFF) > (bdst & 0xFF)) | |||
return 1; | |||
else if ((bsrc ^ bdst) == 0) | |||
return 0; | |||
else | |||
return -1; | |||
} | |||
#endregion | |||
#region 根据当前位置读取4字节 | |||
/**/ | |||
///<summary> | |||
///从当前位置读取4字节,转换为长整型 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<returns></returns> | |||
private long readLong4(long offset) | |||
{ | |||
long ret = 0; | |||
ipFile.Position = offset; | |||
ret |= (ipFile.ReadByte() & 0xFF); | |||
ret |= ((ipFile.ReadByte() << 8) & 0xFF00); | |||
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); | |||
ret |= ((ipFile.ReadByte() << 24) & 0xFF000000); | |||
return ret; | |||
} | |||
#endregion | |||
#region 根据当前位置,读取3字节 | |||
/**/ | |||
///<summary> | |||
///根据当前位置,读取3字节 | |||
///</summary> | |||
///<param name="offset"></param> | |||
///<returns></returns> | |||
private long readLong3(long offset) | |||
{ | |||
long ret = 0; | |||
ipFile.Position = offset; | |||
ret |= (ipFile.ReadByte() & 0xFF); | |||
ret |= ((ipFile.ReadByte() << 8) & 0xFF00); | |||
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); | |||
return ret; | |||
} | |||
#endregion | |||
#region 从当前位置读取3字节 | |||
/**/ | |||
///<summary> | |||
///从当前位置读取3字节 | |||
///</summary> | |||
///<returns></returns> | |||
private long readLong3() | |||
{ | |||
long ret = 0; | |||
ret |= (ipFile.ReadByte() & 0xFF); | |||
ret |= ((ipFile.ReadByte() << 8) & 0xFF00); | |||
ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); | |||
return ret; | |||
} | |||
#endregion | |||
#region 取得begin和end之间的偏移量 | |||
/**/ | |||
///<summary> | |||
///取得begin和end中间的偏移 | |||
///</summary> | |||
///<param name="begin"></param> | |||
///<param name="end"></param> | |||
///<returns></returns> | |||
private long getMiddleOffset(long begin, long end) | |||
{ | |||
long records = (end - begin)/IP_RECORD_LENGTH; | |||
records >>= 1; | |||
if (records == 0) | |||
records = 1; | |||
return begin + records*IP_RECORD_LENGTH; | |||
} | |||
#endregion | |||
} | |||
public class IPLocation | |||
{ | |||
public String country; | |||
public String area; | |||
public IPLocation() | |||
{ | |||
country = area = ""; | |||
} | |||
public IPLocation getCopy() | |||
{ | |||
IPLocation ret = new IPLocation(); | |||
ret.country = country; | |||
ret.area = area; | |||
return ret; | |||
} | |||
} | |||
#region Old | |||
/* | |||
/// <summary> | |||
/// 存储地区的结构 | |||
/// </summary> | |||
public struct stLocation | |||
{ | |||
/// <summary> | |||
/// 未使用 | |||
/// </summary> | |||
public string Ip; | |||
/// <summary> | |||
/// 国家名 | |||
/// </summary> | |||
public string Contry; | |||
/// <summary> | |||
/// 城市名 | |||
/// </summary> | |||
public string City; | |||
} | |||
/// <summary> | |||
/// 纯真IP数据库查询辅助类 | |||
/// </summary> | |||
public static class QqwryHelper | |||
{ | |||
#region 成员变量 | |||
private const byte REDIRECT_MODE_1 = 0x01;//名称存储模式一 | |||
private const byte REDIRECT_MODE_2 = 0x02;//名称存储模式二 | |||
private const int IP_RECORD_LENGTH = 7; //每条索引的长度 | |||
private static long beginIndex = 0;//索引开始 | |||
private static long endIndex = 0;//索引结束 | |||
private static stLocation loc = new stLocation() { City = "未知城市", Contry = "未知国家" }; | |||
private static Stream fs; | |||
#endregion | |||
#region 私有成员函数 | |||
/// <summary> | |||
/// 在索引区查找指定IP对应的记录区地址 | |||
/// </summary> | |||
/// <param name="_ip">字节型IP</param> | |||
/// <returns></returns> | |||
private static long SearchIpIndex(byte[] _ip) | |||
{ | |||
long index = 0; | |||
byte[] nextIp = new byte[4]; | |||
ReadIp(beginIndex, ref nextIp); | |||
int flag = CompareIp(_ip, nextIp); | |||
if (flag == 0) return beginIndex; | |||
else if (flag < 0) return -1; | |||
for (long i = beginIndex, j = endIndex; i < j; ) | |||
{ | |||
index = GetMiddleOffset(i, j); | |||
ReadIp(index, ref nextIp); | |||
flag = CompareIp(_ip, nextIp); | |||
if (flag == 0) return ReadLong(index + 4, 3); | |||
else if (flag > 0) i = index; | |||
else if (flag < 0) | |||
{ | |||
if (index == j) | |||
{ | |||
j -= IP_RECORD_LENGTH; | |||
index = j; | |||
} | |||
else | |||
{ | |||
j = index; | |||
} | |||
} | |||
} | |||
index = ReadLong(index + 4, 3); | |||
ReadIp(index, ref nextIp); | |||
flag = CompareIp(_ip, nextIp); | |||
if (flag <= 0) return index; | |||
else return -1; | |||
} | |||
/// <summary> | |||
/// 获取两个索引的中间位置 | |||
/// </summary> | |||
/// <param name="begin">索引1</param> | |||
/// <param name="end">索引2</param> | |||
/// <returns></returns> | |||
private static long GetMiddleOffset(long begin, long end) | |||
{ | |||
long records = (end - begin) / IP_RECORD_LENGTH; | |||
records >>= 1; | |||
if (records == 0) records = 1; | |||
return begin + records * IP_RECORD_LENGTH; | |||
} | |||
/// <summary> | |||
/// 读取记录区的地区名称 | |||
/// </summary> | |||
/// <param name="offset">位置</param> | |||
/// <returns></returns> | |||
private static string ReadString(long offset) | |||
{ | |||
fs.Position = offset; | |||
byte b = (byte)fs.ReadByte(); | |||
if (b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) | |||
{ | |||
long areaOffset = ReadLong(offset + 1, 3); | |||
if (areaOffset == 0) | |||
return "未知地区"; | |||
else fs.Position = areaOffset; | |||
} | |||
else | |||
{ | |||
fs.Position = offset; | |||
} | |||
List<byte> buf = new List<byte>(); | |||
int i = 0; | |||
for (i = 0, buf.Add((byte)fs.ReadByte()); buf[i] != (byte)(0); ++i, buf.Add((byte)fs.ReadByte())) ; | |||
if (i > 0) return Encoding.Default.GetString(buf.ToArray(), 0, i); | |||
else return ""; | |||
} | |||
/// <summary> | |||
/// 从自定位置读取指定长度的字节,并转换为big-endian字节序(数据源文件为little-endian字节序) | |||
/// </summary> | |||
/// <param name="offset">开始读取位置</param> | |||
/// <param name="length">读取长度</param> | |||
/// <returns></returns> | |||
private static long ReadLong(long offset, int length) | |||
{ | |||
long ret = 0; | |||
fs.Position = offset; | |||
for (int i = 0; i < length; i++) | |||
{ | |||
ret |= ((fs.ReadByte() << (i * 8)) & (0xFF * ((int)Math.Pow(16, i * 2)))); | |||
} | |||
return ret; | |||
} | |||
/// <summary> | |||
/// 从指定位置处读取一个IP | |||
/// </summary> | |||
/// <param name="offset">指定的位置</param> | |||
/// <param name="_buffIp">保存IP的缓存区</param> | |||
private static void ReadIp(long offset, ref byte[] _buffIp) | |||
{ | |||
fs.Position = offset; | |||
fs.Read(_buffIp, 0, _buffIp.Length); | |||
for (int i = 0; i < _buffIp.Length / 2; i++) | |||
{ | |||
byte temp = _buffIp[i]; | |||
_buffIp[i] = _buffIp[_buffIp.Length - i - 1]; | |||
_buffIp[_buffIp.Length - i - 1] = temp; | |||
} | |||
} | |||
/// <summary> | |||
/// 比较两个IP是否相等,1:IP1大于IP2,-1:IP1小于IP2,0:IP1=IP2 | |||
/// </summary> | |||
/// <param name="_buffIp1">IP1</param> | |||
/// <param name="_buffIp2">IP2</param> | |||
/// <returns></returns> | |||
private static int CompareIp(byte[] _buffIp1, byte[] _buffIp2) | |||
{ | |||
if (_buffIp1.Length > 4 || _buffIp2.Length > 4) throw new Exception("指定的IP无效。"); | |||
for (int i = 0; i < 4; i++) | |||
{ | |||
if ((_buffIp1[i] & 0xFF) > (_buffIp2[i] & 0xFF)) return 1; | |||
else if ((_buffIp1[i] & 0xFF) < (_buffIp2[i] & 0xFF)) return -1; | |||
} | |||
return 0; | |||
} | |||
/// <summary> | |||
/// 从指定的地址获取区域名称 | |||
/// </summary> | |||
/// <param name="offset"></param> | |||
private static void GetAreaName(long offset) | |||
{ | |||
fs.Position = offset + 4; | |||
long flag = fs.ReadByte(); | |||
long contryIndex = 0; | |||
if (flag == REDIRECT_MODE_1) | |||
{ | |||
contryIndex = ReadLong(fs.Position, 3); | |||
fs.Position = contryIndex; | |||
flag = fs.ReadByte(); | |||
if (flag == REDIRECT_MODE_2) //是否仍然为重定向 | |||
{ | |||
loc.Contry = ReadString(ReadLong(fs.Position, 3)); | |||
fs.Position = contryIndex + 4; | |||
} | |||
else | |||
{ | |||
loc.Contry = ReadString(contryIndex); | |||
} | |||
loc.City = ReadString(fs.Position); | |||
} | |||
else if (flag == REDIRECT_MODE_2) | |||
{ | |||
contryIndex = ReadLong(fs.Position, 3); | |||
loc.Contry = ReadString(contryIndex); | |||
loc.City = ReadString(contryIndex + 3); | |||
} | |||
else | |||
{ | |||
loc.Contry = ReadString(offset + 4); | |||
loc.City = ReadString(fs.Position); | |||
} | |||
} | |||
#endregion | |||
#region 公有成员函数 | |||
/// <summary> | |||
/// 加载数据库文件到缓存 | |||
/// </summary> | |||
/// <param name="path">数据库文件地址</param> | |||
/// <returns></returns> | |||
public static void Init(string path) | |||
{ | |||
if (fs != null) return; | |||
var bt = File.ReadAllBytes(path); | |||
fs = new MemoryStream(bt); | |||
} | |||
/// <summary> | |||
/// 根据IP获取区域名 | |||
/// </summary> | |||
/// <param name="ip">指定的IP</param> | |||
/// <returns></returns> | |||
public static stLocation GetLocation(string ip) | |||
{ | |||
IPAddress ipAddress = null; | |||
if (!IPAddress.TryParse(ip, out ipAddress)) throw new Exception("无效的IP地址。"); | |||
byte[] buff_local_ip = ipAddress.GetAddressBytes(); | |||
beginIndex = ReadLong(0, 4); | |||
endIndex = ReadLong(4, 4); | |||
long offset = SearchIpIndex(buff_local_ip); | |||
if (offset != -1) | |||
{ | |||
GetAreaName(offset); | |||
} | |||
loc.Contry = loc.Contry.Trim(); | |||
loc.City = loc.City.Trim().Replace("CZ88.NET", ""); | |||
return loc; | |||
} | |||
/// <summary> | |||
/// 释放资源 | |||
/// </summary> | |||
public static void Dispose() | |||
{ | |||
fs.Dispose(); | |||
} | |||
#endregion | |||
} */ | |||
#endregion | |||
} |
@@ -29,9 +29,6 @@ namespace Shadowsocks.Controller | |||
public static void UncompressFile(string fileName, byte[] content) | |||
{ | |||
//some people use RamDisk, their Temp dir maybe disappeared, so we need check it first | |||
if (!Directory.Exists(Path.GetDirectoryName(fileName)))Directory.CreateDirectory(Path.GetDirectoryName(fileName)); | |||
FileStream destinationFile = File.Create(fileName); | |||
// Because the uncompressed size of the file is unknown, | |||
@@ -12,7 +12,7 @@ namespace Shadowsocks.Controller | |||
static I18N() | |||
{ | |||
Strings = new Dictionary<string, string>(); | |||
if (System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag.ToLowerInvariant().StartsWith("zh")) | |||
{ | |||
string[] lines = Regex.Split(Resources.cn, "\r\n|\r|\n"); | |||
@@ -1,4 +1,5 @@ | |||
using System; | |||
using Shadowsocks.Util; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Net.Sockets; | |||
@@ -14,14 +15,14 @@ namespace Shadowsocks.Controller | |||
{ | |||
try | |||
{ | |||
string temppath = Path.GetTempPath(); | |||
string temppath = Utils.GetTempPath(); | |||
LogFile = Path.Combine(temppath, "shadowsocks.log"); | |||
FileStream fs = new FileStream(LogFile, FileMode.Append); | |||
StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs); | |||
sw.AutoFlush = true; | |||
Console.SetOut(sw); | |||
Console.SetError(sw); | |||
return true; | |||
} | |||
catch (IOException e) | |||
@@ -31,6 +32,14 @@ namespace Shadowsocks.Controller | |||
} | |||
} | |||
public static void Debug(object o) | |||
{ | |||
#if DEBUG | |||
Console.WriteLine(o); | |||
#endif | |||
} | |||
public static void LogUsefulException(Exception e) | |||
{ | |||
// just log useful exceptions, not all of them | |||
@@ -55,12 +64,14 @@ namespace Shadowsocks.Controller | |||
Console.WriteLine(e); | |||
} | |||
} | |||
else if (e is ObjectDisposedException) | |||
{ | |||
} | |||
else | |||
{ | |||
Console.WriteLine(e); | |||
} | |||
} | |||
} | |||
// Simply extended System.IO.StreamWriter for adding timestamp workaround | |||
@@ -0,0 +1,117 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Net.NetworkInformation; | |||
using System.Threading; | |||
using Shadowsocks.Model; | |||
using System.Reflection; | |||
using Shadowsocks.Util; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class AvailabilityStatistics | |||
{ | |||
private static readonly string StatisticsFilesName = "shadowsocks.availability.csv"; | |||
private static readonly string Delimiter = ","; | |||
private static readonly int Timeout = 500; | |||
private static readonly int Repeat = 4; //repeat times every evaluation | |||
private static readonly int Interval = 10 * 60 * 1000; //evaluate proxies every 15 minutes | |||
private Timer timer = null; | |||
private State state = null; | |||
private List<Server> servers; | |||
public static string AvailabilityStatisticsFile; | |||
//static constructor to initialize every public static fields before refereced | |||
static AvailabilityStatistics() | |||
{ | |||
string temppath = Utils.GetTempPath(); | |||
AvailabilityStatisticsFile = Path.Combine(temppath, StatisticsFilesName); | |||
} | |||
public bool Set(bool enabled) | |||
{ | |||
try | |||
{ | |||
if (enabled) | |||
{ | |||
if (timer?.Change(0, Interval) == null) | |||
{ | |||
state = new State(); | |||
timer = new Timer(Evaluate, state, 0, Interval); | |||
} | |||
} | |||
else | |||
{ | |||
timer?.Dispose(); | |||
} | |||
return true; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
return false; | |||
} | |||
} | |||
private void Evaluate(object obj) | |||
{ | |||
Ping ping = new Ping(); | |||
State state = (State) obj; | |||
foreach (var server in servers) | |||
{ | |||
Logging.Debug("eveluating " + server.FriendlyName()); | |||
foreach (var _ in Enumerable.Range(0, Repeat)) | |||
{ | |||
//TODO: do simple analyze of data to provide friendly message, like package loss. | |||
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); | |||
//ICMP echo. we can also set options and special bytes | |||
//seems no need to use SendPingAsync | |||
try | |||
{ | |||
PingReply reply = ping.Send(server.server, Timeout); | |||
state.data = new List<KeyValuePair<string, string>>(); | |||
state.data.Add(new KeyValuePair<string, string>("Timestamp", timestamp)); | |||
state.data.Add(new KeyValuePair<string, string>("Server", server.FriendlyName())); | |||
state.data.Add(new KeyValuePair<string, string>("Status", reply.Status.ToString())); | |||
state.data.Add(new KeyValuePair<string, string>("RoundtripTime", reply.RoundtripTime.ToString())); | |||
//state.data.Add(new KeyValuePair<string, string>("data", reply.Buffer.ToString())); // The data of reply | |||
Append(state.data); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
} | |||
} | |||
private static void Append(List<KeyValuePair<string, string>> data) | |||
{ | |||
string dataLine = string.Join(Delimiter, data.Select(kv => kv.Value).ToArray()); | |||
string[] lines; | |||
if (!File.Exists(AvailabilityStatisticsFile)) | |||
{ | |||
string headerLine = string.Join(Delimiter, data.Select(kv => kv.Key).ToArray()); | |||
lines = new string[] { headerLine, dataLine }; | |||
} | |||
else | |||
{ | |||
lines = new string[] { dataLine }; | |||
} | |||
File.AppendAllLines(AvailabilityStatisticsFile, lines); | |||
} | |||
internal void UpdateConfiguration(Configuration _config) | |||
{ | |||
Set(_config.availabilityStatistics); | |||
servers = _config.configs; | |||
} | |||
private class State | |||
{ | |||
public List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>(); | |||
} | |||
} | |||
} |
@@ -1,100 +1,100 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Net; | |||
using System.IO; | |||
using Shadowsocks.Properties; | |||
using SimpleJson; | |||
using Shadowsocks.Util; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class GFWListUpdater | |||
{ | |||
private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt"; | |||
private static string PAC_FILE = PACServer.PAC_FILE; | |||
private static string USER_RULE_FILE = PACServer.USER_RULE_FILE; | |||
public event EventHandler<ResultEventArgs> UpdateCompleted; | |||
public event ErrorEventHandler Error; | |||
public class ResultEventArgs : EventArgs | |||
{ | |||
public bool Success; | |||
public ResultEventArgs(bool success) | |||
{ | |||
this.Success = success; | |||
} | |||
} | |||
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) | |||
{ | |||
try | |||
{ | |||
List<string> lines = ParseResult(e.Result); | |||
if (File.Exists(USER_RULE_FILE)) | |||
{ | |||
string local = File.ReadAllText(USER_RULE_FILE, Encoding.UTF8); | |||
string[] rules = local.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); | |||
foreach(string rule in rules) | |||
{ | |||
if (rule.StartsWith("!") || rule.StartsWith("[")) | |||
continue; | |||
lines.Add(rule); | |||
} | |||
} | |||
string abpContent = Utils.UnGzip(Resources.abp_js); | |||
abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines)); | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
string original = File.ReadAllText(PAC_FILE, Encoding.UTF8); | |||
if (original == abpContent) | |||
{ | |||
UpdateCompleted(this, new ResultEventArgs(false)); | |||
return; | |||
} | |||
} | |||
File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8); | |||
if (UpdateCompleted != null) | |||
{ | |||
UpdateCompleted(this, new ResultEventArgs(true)); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
if (Error != null) | |||
{ | |||
Error(this, new ErrorEventArgs(ex)); | |||
} | |||
} | |||
} | |||
public void UpdatePACFromGFWList(Configuration config) | |||
{ | |||
WebClient http = new WebClient(); | |||
http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); | |||
http.DownloadStringCompleted += http_DownloadStringCompleted; | |||
http.DownloadStringAsync(new Uri(GFWLIST_URL)); | |||
} | |||
public List<string> ParseResult(string response) | |||
{ | |||
byte[] bytes = Convert.FromBase64String(response); | |||
string content = Encoding.ASCII.GetString(bytes); | |||
string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); | |||
List<string> valid_lines = new List<string>(lines.Length); | |||
foreach (string line in lines) | |||
{ | |||
if (line.StartsWith("!") || line.StartsWith("[")) | |||
continue; | |||
valid_lines.Add(line); | |||
} | |||
return valid_lines; | |||
} | |||
} | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Net; | |||
using System.IO; | |||
using Shadowsocks.Properties; | |||
using SimpleJson; | |||
using Shadowsocks.Util; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class GFWListUpdater | |||
{ | |||
private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt"; | |||
private static string PAC_FILE = PACServer.PAC_FILE; | |||
private static string USER_RULE_FILE = PACServer.USER_RULE_FILE; | |||
public event EventHandler<ResultEventArgs> UpdateCompleted; | |||
public event ErrorEventHandler Error; | |||
public class ResultEventArgs : EventArgs | |||
{ | |||
public bool Success; | |||
public ResultEventArgs(bool success) | |||
{ | |||
this.Success = success; | |||
} | |||
} | |||
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) | |||
{ | |||
try | |||
{ | |||
List<string> lines = ParseResult(e.Result); | |||
if (File.Exists(USER_RULE_FILE)) | |||
{ | |||
string local = File.ReadAllText(USER_RULE_FILE, Encoding.UTF8); | |||
string[] rules = local.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); | |||
foreach(string rule in rules) | |||
{ | |||
if (rule.StartsWith("!") || rule.StartsWith("[")) | |||
continue; | |||
lines.Add(rule); | |||
} | |||
} | |||
string abpContent = Utils.UnGzip(Resources.abp_js); | |||
abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines)); | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
string original = File.ReadAllText(PAC_FILE, Encoding.UTF8); | |||
if (original == abpContent) | |||
{ | |||
UpdateCompleted(this, new ResultEventArgs(false)); | |||
return; | |||
} | |||
} | |||
File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8); | |||
if (UpdateCompleted != null) | |||
{ | |||
UpdateCompleted(this, new ResultEventArgs(true)); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
if (Error != null) | |||
{ | |||
Error(this, new ErrorEventArgs(ex)); | |||
} | |||
} | |||
} | |||
public void UpdatePACFromGFWList(Configuration config) | |||
{ | |||
WebClient http = new WebClient(); | |||
http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); | |||
http.DownloadStringCompleted += http_DownloadStringCompleted; | |||
http.DownloadStringAsync(new Uri(GFWLIST_URL)); | |||
} | |||
public List<string> ParseResult(string response) | |||
{ | |||
byte[] bytes = Convert.FromBase64String(response); | |||
string content = Encoding.ASCII.GetString(bytes); | |||
string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); | |||
List<string> valid_lines = new List<string>(lines.Length); | |||
foreach (string line in lines) | |||
{ | |||
if (line.StartsWith("!") || line.StartsWith("[")) | |||
continue; | |||
valid_lines.Add(line); | |||
} | |||
return valid_lines; | |||
} | |||
} | |||
} |
@@ -1,163 +1,216 @@ | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Net.NetworkInformation; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class Listener | |||
{ | |||
public interface Service | |||
{ | |||
bool Handle(byte[] firstPacket, int length, Socket socket); | |||
} | |||
Configuration _config; | |||
bool _shareOverLAN; | |||
Socket _socket; | |||
IList<Service> _services; | |||
public Listener(IList<Service> services) | |||
{ | |||
this._services = services; | |||
} | |||
private bool CheckIfPortInUse(int port) | |||
{ | |||
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); | |||
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); | |||
foreach (IPEndPoint endPoint in ipEndPoints) | |||
{ | |||
if (endPoint.Port == port) | |||
{ | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
public void Start(Configuration config) | |||
{ | |||
this._config = config; | |||
this._shareOverLAN = config.shareOverLan; | |||
if (CheckIfPortInUse(_config.localPort)) | |||
throw new Exception(I18N.GetString("Port already in use")); | |||
try | |||
{ | |||
// Create a TCP/IP socket. | |||
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |||
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | |||
IPEndPoint localEndPoint = null; | |||
if (_shareOverLAN) | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort); | |||
} | |||
else | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort); | |||
} | |||
// Bind the socket to the local endpoint and listen for incoming connections. | |||
_socket.Bind(localEndPoint); | |||
_socket.Listen(1024); | |||
// Start an asynchronous socket to listen for connections. | |||
Console.WriteLine("Shadowsocks started"); | |||
_socket.BeginAccept( | |||
new AsyncCallback(AcceptCallback), | |||
_socket); | |||
} | |||
catch (SocketException) | |||
{ | |||
_socket.Close(); | |||
throw; | |||
} | |||
} | |||
public void Stop() | |||
{ | |||
if (_socket != null) | |||
{ | |||
_socket.Close(); | |||
_socket = null; | |||
} | |||
} | |||
public void AcceptCallback(IAsyncResult ar) | |||
{ | |||
Socket listener = (Socket)ar.AsyncState; | |||
try | |||
{ | |||
Socket conn = listener.EndAccept(ar); | |||
byte[] buf = new byte[4096]; | |||
object[] state = new object[] { | |||
conn, | |||
buf | |||
}; | |||
conn.BeginReceive(buf, 0, buf.Length, 0, | |||
new AsyncCallback(ReceiveCallback), state); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
} | |||
finally | |||
{ | |||
try | |||
{ | |||
listener.BeginAccept( | |||
new AsyncCallback(AcceptCallback), | |||
listener); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
// do nothing | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
} | |||
private void ReceiveCallback(IAsyncResult ar) | |||
{ | |||
object[] state = (object[])ar.AsyncState; | |||
Socket conn = (Socket)state[0]; | |||
byte[] buf = (byte[])state[1]; | |||
try | |||
{ | |||
int bytesRead = conn.EndReceive(ar); | |||
foreach (Service service in _services) | |||
{ | |||
if (service.Handle(buf, bytesRead, conn)) | |||
{ | |||
return; | |||
} | |||
} | |||
// no service found for this | |||
// shouldn't happen | |||
conn.Close(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
conn.Close(); | |||
} | |||
} | |||
} | |||
} | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Net.NetworkInformation; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class Listener | |||
{ | |||
public interface Service | |||
{ | |||
bool Handle(byte[] firstPacket, int length, Socket socket, object state); | |||
} | |||
public class UDPState | |||
{ | |||
public byte[] buffer = new byte[4096]; | |||
public EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | |||
} | |||
Configuration _config; | |||
bool _shareOverLAN; | |||
Socket _tcpSocket; | |||
Socket _udpSocket; | |||
IList<Service> _services; | |||
public Listener(IList<Service> services) | |||
{ | |||
this._services = services; | |||
} | |||
private bool CheckIfPortInUse(int port) | |||
{ | |||
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); | |||
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); | |||
foreach (IPEndPoint endPoint in ipEndPoints) | |||
{ | |||
if (endPoint.Port == port) | |||
{ | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
public void Start(Configuration config) | |||
{ | |||
this._config = config; | |||
this._shareOverLAN = config.shareOverLan; | |||
if (CheckIfPortInUse(_config.localPort)) | |||
throw new Exception(I18N.GetString("Port already in use")); | |||
try | |||
{ | |||
// Create a TCP/IP socket. | |||
_tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |||
_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | |||
_tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | |||
_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | |||
IPEndPoint localEndPoint = null; | |||
if (_shareOverLAN) | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort); | |||
} | |||
else | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort); | |||
} | |||
// Bind the socket to the local endpoint and listen for incoming connections. | |||
_tcpSocket.Bind(localEndPoint); | |||
_udpSocket.Bind(localEndPoint); | |||
_tcpSocket.Listen(1024); | |||
// Start an asynchronous socket to listen for connections. | |||
Console.WriteLine("Shadowsocks started"); | |||
_tcpSocket.BeginAccept( | |||
new AsyncCallback(AcceptCallback), | |||
_tcpSocket); | |||
UDPState udpState = new UDPState(); | |||
_udpSocket.BeginReceiveFrom(udpState.buffer, 0, udpState.buffer.Length, 0, ref udpState.remoteEndPoint, new AsyncCallback(RecvFromCallback), udpState); | |||
} | |||
catch (SocketException) | |||
{ | |||
_tcpSocket.Close(); | |||
throw; | |||
} | |||
} | |||
public void Stop() | |||
{ | |||
if (_tcpSocket != null) | |||
{ | |||
_tcpSocket.Close(); | |||
_tcpSocket = null; | |||
} | |||
if (_udpSocket != null) | |||
{ | |||
_udpSocket.Close(); | |||
_udpSocket = null; | |||
} | |||
} | |||
public void RecvFromCallback(IAsyncResult ar) | |||
{ | |||
UDPState state = (UDPState)ar.AsyncState; | |||
try | |||
{ | |||
int bytesRead = _udpSocket.EndReceiveFrom(ar, ref state.remoteEndPoint); | |||
foreach (Service service in _services) | |||
{ | |||
if (service.Handle(state.buffer, bytesRead, _udpSocket, state)) | |||
{ | |||
break; | |||
} | |||
} | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
} | |||
catch (Exception) | |||
{ | |||
} | |||
finally | |||
{ | |||
try | |||
{ | |||
_udpSocket.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, 0, ref state.remoteEndPoint, new AsyncCallback(RecvFromCallback), state); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
// do nothing | |||
} | |||
catch (Exception) | |||
{ | |||
} | |||
} | |||
} | |||
public void AcceptCallback(IAsyncResult ar) | |||
{ | |||
Socket listener = (Socket)ar.AsyncState; | |||
try | |||
{ | |||
Socket conn = listener.EndAccept(ar); | |||
byte[] buf = new byte[4096]; | |||
object[] state = new object[] { | |||
conn, | |||
buf | |||
}; | |||
conn.BeginReceive(buf, 0, buf.Length, 0, | |||
new AsyncCallback(ReceiveCallback), state); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
} | |||
finally | |||
{ | |||
try | |||
{ | |||
listener.BeginAccept( | |||
new AsyncCallback(AcceptCallback), | |||
listener); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
// do nothing | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
} | |||
private void ReceiveCallback(IAsyncResult ar) | |||
{ | |||
object[] state = (object[])ar.AsyncState; | |||
Socket conn = (Socket)state[0]; | |||
byte[] buf = (byte[])state[1]; | |||
try | |||
{ | |||
int bytesRead = conn.EndReceive(ar); | |||
foreach (Service service in _services) | |||
{ | |||
if (service.Handle(buf, bytesRead, conn, null)) | |||
{ | |||
return; | |||
} | |||
} | |||
// no service found for this | |||
if (conn.ProtocolType == ProtocolType.Tcp) | |||
{ | |||
conn.Close(); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
conn.Close(); | |||
} | |||
} | |||
} | |||
} |
@@ -1,207 +1,210 @@ | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks.Util; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PACServer : Listener.Service | |||
{ | |||
public static string PAC_FILE = "pac.txt"; | |||
public static string USER_RULE_FILE = "user-rule.txt"; | |||
FileSystemWatcher watcher; | |||
private Configuration _config; | |||
public event EventHandler PACFileChanged; | |||
public PACServer() | |||
{ | |||
this.WatchPacFile(); | |||
} | |||
public void UpdateConfiguration(Configuration config) | |||
{ | |||
this._config = config; | |||
} | |||
public bool Handle(byte[] firstPacket, int length, Socket socket) | |||
{ | |||
try | |||
{ | |||
string request = Encoding.UTF8.GetString(firstPacket, 0, length); | |||
string[] lines = request.Split('\r', '\n'); | |||
bool hostMatch = false, pathMatch = false, useSocks = false; | |||
foreach (string line in lines) | |||
{ | |||
string[] kv = line.Split(new char[]{':'}, 2); | |||
if (kv.Length == 2) | |||
{ | |||
if (kv[0] == "Host") | |||
{ | |||
if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString()) | |||
{ | |||
hostMatch = true; | |||
} | |||
} | |||
else if (kv[0] == "User-Agent") | |||
{ | |||
// we need to drop connections when changing servers | |||
/* if (kv[1].IndexOf("Chrome") >= 0) | |||
{ | |||
useSocks = true; | |||
} */ | |||
} | |||
} | |||
else if (kv.Length == 1) | |||
{ | |||
if (line.IndexOf("pac") >= 0) | |||
{ | |||
pathMatch = true; | |||
} | |||
} | |||
} | |||
if (hostMatch && pathMatch) | |||
{ | |||
SendResponse(firstPacket, length, socket, useSocks); | |||
return true; | |||
} | |||
return false; | |||
} | |||
catch (ArgumentException) | |||
{ | |||
return false; | |||
} | |||
} | |||
public string TouchPACFile() | |||
{ | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
return PAC_FILE; | |||
} | |||
else | |||
{ | |||
FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt); | |||
return PAC_FILE; | |||
} | |||
} | |||
internal string TouchUserRuleFile() | |||
{ | |||
if (File.Exists(USER_RULE_FILE)) | |||
{ | |||
return USER_RULE_FILE; | |||
} | |||
else | |||
{ | |||
File.WriteAllText(USER_RULE_FILE, Resources.user_rule); | |||
return USER_RULE_FILE; | |||
} | |||
} | |||
private string GetPACContent() | |||
{ | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
return File.ReadAllText(PAC_FILE, Encoding.UTF8); | |||
} | |||
else | |||
{ | |||
return Utils.UnGzip(Resources.proxy_pac_txt); | |||
} | |||
} | |||
public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks) | |||
{ | |||
try | |||
{ | |||
string pac = GetPACContent(); | |||
IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; | |||
string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks); | |||
pac = pac.Replace("__PROXY__", proxy); | |||
string text = String.Format(@"HTTP/1.1 200 OK | |||
Server: Shadowsocks | |||
Content-Type: application/x-ns-proxy-autoconfig | |||
Content-Length: {0} | |||
Connection: Close | |||
", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac; | |||
byte[] response = System.Text.Encoding.UTF8.GetBytes(text); | |||
socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket); | |||
Util.Utils.ReleaseMemory(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
socket.Close(); | |||
} | |||
} | |||
private void SendCallback(IAsyncResult ar) | |||
{ | |||
Socket conn = (Socket)ar.AsyncState; | |||
try | |||
{ | |||
conn.Shutdown(SocketShutdown.Send); | |||
} | |||
catch | |||
{ } | |||
} | |||
private void WatchPacFile() | |||
{ | |||
if (watcher != null) | |||
{ | |||
watcher.Dispose(); | |||
} | |||
watcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); | |||
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; | |||
watcher.Filter = PAC_FILE; | |||
watcher.Changed += Watcher_Changed; | |||
watcher.Created += Watcher_Changed; | |||
watcher.Deleted += Watcher_Changed; | |||
watcher.Renamed += Watcher_Changed; | |||
watcher.EnableRaisingEvents = true; | |||
} | |||
private void Watcher_Changed(object sender, FileSystemEventArgs e) | |||
{ | |||
if (PACFileChanged != null) | |||
{ | |||
PACFileChanged(this, new EventArgs()); | |||
} | |||
} | |||
private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks) | |||
{ | |||
//try | |||
//{ | |||
// string requestString = Encoding.UTF8.GetString(requestBuf); | |||
// if (requestString.IndexOf("AppleWebKit") >= 0) | |||
// { | |||
// string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port; | |||
// proxy = "SOCKS5 " + address + "; SOCKS " + address + ";"; | |||
// } | |||
//} | |||
//catch (Exception e) | |||
//{ | |||
// Console.WriteLine(e); | |||
//} | |||
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";"; | |||
} | |||
} | |||
} | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks.Util; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PACServer : Listener.Service | |||
{ | |||
public static string PAC_FILE = "pac.txt"; | |||
public static string USER_RULE_FILE = "user-rule.txt"; | |||
FileSystemWatcher watcher; | |||
private Configuration _config; | |||
public event EventHandler PACFileChanged; | |||
public PACServer() | |||
{ | |||
this.WatchPacFile(); | |||
} | |||
public void UpdateConfiguration(Configuration config) | |||
{ | |||
this._config = config; | |||
} | |||
public bool Handle(byte[] firstPacket, int length, Socket socket, object state) | |||
{ | |||
if (socket.ProtocolType != ProtocolType.Tcp) | |||
{ | |||
return false; | |||
} | |||
try | |||
{ | |||
string request = Encoding.UTF8.GetString(firstPacket, 0, length); | |||
string[] lines = request.Split('\r', '\n'); | |||
bool hostMatch = false, pathMatch = false, useSocks = false; | |||
foreach (string line in lines) | |||
{ | |||
string[] kv = line.Split(new char[]{':'}, 2); | |||
if (kv.Length == 2) | |||
{ | |||
if (kv[0] == "Host") | |||
{ | |||
if (kv[1].Trim() == ((IPEndPoint)socket.LocalEndPoint).ToString()) | |||
{ | |||
hostMatch = true; | |||
} | |||
} | |||
else if (kv[0] == "User-Agent") | |||
{ | |||
// we need to drop connections when changing servers | |||
/* if (kv[1].IndexOf("Chrome") >= 0) | |||
{ | |||
useSocks = true; | |||
} */ | |||
} | |||
} | |||
else if (kv.Length == 1) | |||
{ | |||
if (line.IndexOf("pac") >= 0) | |||
{ | |||
pathMatch = true; | |||
} | |||
} | |||
} | |||
if (hostMatch && pathMatch) | |||
{ | |||
SendResponse(firstPacket, length, socket, useSocks); | |||
return true; | |||
} | |||
return false; | |||
} | |||
catch (ArgumentException) | |||
{ | |||
return false; | |||
} | |||
} | |||
public string TouchPACFile() | |||
{ | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
return PAC_FILE; | |||
} | |||
else | |||
{ | |||
FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt); | |||
return PAC_FILE; | |||
} | |||
} | |||
internal string TouchUserRuleFile() | |||
{ | |||
if (File.Exists(USER_RULE_FILE)) | |||
{ | |||
return USER_RULE_FILE; | |||
} | |||
else | |||
{ | |||
File.WriteAllText(USER_RULE_FILE, Resources.user_rule); | |||
return USER_RULE_FILE; | |||
} | |||
} | |||
private string GetPACContent() | |||
{ | |||
if (File.Exists(PAC_FILE)) | |||
{ | |||
return File.ReadAllText(PAC_FILE, Encoding.UTF8); | |||
} | |||
else | |||
{ | |||
return Utils.UnGzip(Resources.proxy_pac_txt); | |||
} | |||
} | |||
public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks) | |||
{ | |||
try | |||
{ | |||
string pac = GetPACContent(); | |||
IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; | |||
string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks); | |||
pac = pac.Replace("__PROXY__", proxy); | |||
string text = String.Format(@"HTTP/1.1 200 OK | |||
Server: Shadowsocks | |||
Content-Type: application/x-ns-proxy-autoconfig | |||
Content-Length: {0} | |||
Connection: Close | |||
", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac; | |||
byte[] response = System.Text.Encoding.UTF8.GetBytes(text); | |||
socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket); | |||
Util.Utils.ReleaseMemory(true); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
socket.Close(); | |||
} | |||
} | |||
private void SendCallback(IAsyncResult ar) | |||
{ | |||
Socket conn = (Socket)ar.AsyncState; | |||
try | |||
{ | |||
conn.Shutdown(SocketShutdown.Send); | |||
} | |||
catch | |||
{ } | |||
} | |||
private void WatchPacFile() | |||
{ | |||
if (watcher != null) | |||
{ | |||
watcher.Dispose(); | |||
} | |||
watcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); | |||
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; | |||
watcher.Filter = PAC_FILE; | |||
watcher.Changed += Watcher_Changed; | |||
watcher.Created += Watcher_Changed; | |||
watcher.Deleted += Watcher_Changed; | |||
watcher.Renamed += Watcher_Changed; | |||
watcher.EnableRaisingEvents = true; | |||
} | |||
private void Watcher_Changed(object sender, FileSystemEventArgs e) | |||
{ | |||
if (PACFileChanged != null) | |||
{ | |||
PACFileChanged(this, new EventArgs()); | |||
} | |||
} | |||
private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks) | |||
{ | |||
//try | |||
//{ | |||
// string requestString = Encoding.UTF8.GetString(requestBuf); | |||
// if (requestString.IndexOf("AppleWebKit") >= 0) | |||
// { | |||
// string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port; | |||
// proxy = "SOCKS5 " + address + "; SOCKS " + address + ";"; | |||
// } | |||
//} | |||
//catch (Exception e) | |||
//{ | |||
// Console.WriteLine(e); | |||
//} | |||
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";"; | |||
} | |||
} | |||
} |
@@ -1,126 +1,177 @@ | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Text; | |||
using System.Net.NetworkInformation; | |||
using System.Net; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PolipoRunner | |||
{ | |||
private Process _process; | |||
private static string temppath; | |||
private int _runningPort; | |||
static PolipoRunner() | |||
{ | |||
temppath = Path.GetTempPath(); | |||
try | |||
{ | |||
FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); | |||
} | |||
catch (IOException e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
public int RunningPort | |||
{ | |||
get | |||
{ | |||
return _runningPort; | |||
} | |||
} | |||
public void Start(Configuration configuration) | |||
{ | |||
Server server = configuration.GetCurrentServer(); | |||
if (_process == null) | |||
{ | |||
Process[] existingPolipo = Process.GetProcessesByName("ss_polipo"); | |||
foreach (Process p in existingPolipo) | |||
{ | |||
try | |||
{ | |||
p.Kill(); | |||
p.WaitForExit(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e.ToString()); | |||
} | |||
} | |||
string polipoConfig = Resources.polipo_config; | |||
_runningPort = this.GetFreePort(); | |||
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); | |||
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString()); | |||
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); | |||
FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); | |||
_process = new Process(); | |||
// Configure the process using the StartInfo properties. | |||
_process.StartInfo.FileName = temppath + "/ss_polipo.exe"; | |||
_process.StartInfo.Arguments = "-c \"" + temppath + "/polipo.conf\""; | |||
_process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; | |||
_process.StartInfo.UseShellExecute = true; | |||
_process.StartInfo.CreateNoWindow = true; | |||
//_process.StartInfo.RedirectStandardOutput = true; | |||
//_process.StartInfo.RedirectStandardError = true; | |||
_process.Start(); | |||
} | |||
} | |||
public void Stop() | |||
{ | |||
if (_process != null) | |||
{ | |||
try | |||
{ | |||
_process.Kill(); | |||
_process.WaitForExit(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e.ToString()); | |||
} | |||
_process = null; | |||
} | |||
} | |||
private int GetFreePort() | |||
{ | |||
int defaultPort = 8123; | |||
try | |||
{ | |||
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); | |||
IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); | |||
List<int> usedPorts = new List<int>(); | |||
foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()) | |||
{ | |||
usedPorts.Add(endPoint.Port); | |||
} | |||
for (int port = defaultPort; port <= 65535; port++) | |||
{ | |||
if (!usedPorts.Contains(port)) | |||
{ | |||
return port; | |||
} | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
// in case access denied | |||
Logging.LogUsefulException(e); | |||
return defaultPort; | |||
} | |||
throw new Exception("No free port found."); | |||
} | |||
} | |||
} | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Text; | |||
using System.Net.NetworkInformation; | |||
using System.Net; | |||
using System.Runtime.InteropServices; | |||
using Shadowsocks.Util; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PolipoRunner | |||
{ | |||
private Process _process; | |||
private static string temppath; | |||
private int _runningPort; | |||
static PolipoRunner() | |||
{ | |||
temppath = Utils.GetTempPath(); | |||
try | |||
{ | |||
FileManager.UncompressFile(temppath + "/ss_privoxy.exe", Resources.privoxy_exe); | |||
FileManager.UncompressFile(temppath + "/mgwz.dll", Resources.mgwz_dll); | |||
} | |||
catch (IOException e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
public int RunningPort | |||
{ | |||
get | |||
{ | |||
return _runningPort; | |||
} | |||
} | |||
public void Start(Configuration configuration) | |||
{ | |||
Server server = configuration.GetCurrentServer(); | |||
if (_process == null) | |||
{ | |||
Process[] existingPolipo = Process.GetProcessesByName("ss_privoxy"); | |||
foreach (Process p in existingPolipo) | |||
{ | |||
try | |||
{ | |||
p.Kill(); | |||
p.WaitForExit(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e.ToString()); | |||
} | |||
} | |||
string polipoConfig = Resources.privoxy_conf; | |||
_runningPort = this.GetFreePort(); | |||
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); | |||
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_PORT__", _runningPort.ToString()); | |||
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1"); | |||
FileManager.ByteArrayToFile(temppath + "/privoxy.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); | |||
if (!(temppath.EndsWith("\\") || temppath.EndsWith("/"))) { | |||
temppath = temppath + "\\"; | |||
} | |||
_process = new Process(); | |||
// Configure the process using the StartInfo properties. | |||
_process.StartInfo.FileName = temppath + "ss_privoxy.exe"; | |||
_process.StartInfo.Arguments = " \"" + temppath + "privoxy.conf\""; | |||
_process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; | |||
_process.StartInfo.UseShellExecute = true; | |||
_process.StartInfo.CreateNoWindow = true; | |||
//_process.StartInfo.RedirectStandardOutput = true; | |||
//_process.StartInfo.RedirectStandardError = true; | |||
_process.Start(); | |||
} | |||
RefreshTrayArea(); | |||
} | |||
public void Stop() | |||
{ | |||
if (_process != null) | |||
{ | |||
try | |||
{ | |||
_process.Kill(); | |||
_process.WaitForExit(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e.ToString()); | |||
} | |||
_process = null; | |||
} | |||
RefreshTrayArea(); | |||
} | |||
private int GetFreePort() | |||
{ | |||
int defaultPort = 8123; | |||
try | |||
{ | |||
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); | |||
IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); | |||
List<int> usedPorts = new List<int>(); | |||
foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()) | |||
{ | |||
usedPorts.Add(endPoint.Port); | |||
} | |||
for (int port = defaultPort; port <= 65535; port++) | |||
{ | |||
if (!usedPorts.Contains(port)) | |||
{ | |||
return port; | |||
} | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
// in case access denied | |||
Logging.LogUsefulException(e); | |||
return defaultPort; | |||
} | |||
throw new Exception("No free port found."); | |||
} | |||
[StructLayout(LayoutKind.Sequential)] | |||
public struct RECT | |||
{ | |||
public int left; | |||
public int top; | |||
public int right; | |||
public int bottom; | |||
} | |||
[DllImport("user32.dll")] | |||
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |||
[DllImport("user32.dll")] | |||
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); | |||
[DllImport("user32.dll")] | |||
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); | |||
[DllImport("user32.dll")] | |||
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam); | |||
public void RefreshTrayArea() | |||
{ | |||
IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null); | |||
IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null); | |||
IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null); | |||
IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area"); | |||
if (notificationAreaHandle == IntPtr.Zero) | |||
{ | |||
notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area"); | |||
IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null); | |||
IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area"); | |||
RefreshTrayArea(overflowNotificationAreaHandle); | |||
} | |||
RefreshTrayArea(notificationAreaHandle); | |||
} | |||
private static void RefreshTrayArea(IntPtr windowHandle) | |||
{ | |||
const uint wmMousemove = 0x0200; | |||
RECT rect; | |||
GetClientRect(windowHandle, out rect); | |||
for (var x = 0; x < rect.right; x += 5) | |||
for (var y = 0; y < rect.bottom; y += 5) | |||
SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x); | |||
} | |||
} | |||
} |
@@ -1,264 +1,267 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PortForwarder : Listener.Service | |||
{ | |||
int _targetPort; | |||
public PortForwarder(int targetPort) | |||
{ | |||
this._targetPort = targetPort; | |||
} | |||
public bool Handle(byte[] firstPacket, int length, Socket socket) | |||
{ | |||
new Handler().Start(firstPacket, length, socket, this._targetPort); | |||
return true; | |||
} | |||
class Handler | |||
{ | |||
private byte[] _firstPacket; | |||
private int _firstPacketLength; | |||
private Socket _local; | |||
private Socket _remote; | |||
private bool _closed = false; | |||
private bool _localShutdown = false; | |||
private bool _remoteShutdown = false; | |||
public const int RecvSize = 16384; | |||
// remote receive buffer | |||
private byte[] remoteRecvBuffer = new byte[RecvSize]; | |||
// connection receive buffer | |||
private byte[] connetionRecvBuffer = new byte[RecvSize]; | |||
public void Start(byte[] firstPacket, int length, Socket socket, int targetPort) | |||
{ | |||
this._firstPacket = firstPacket; | |||
this._firstPacketLength = length; | |||
this._local = socket; | |||
try | |||
{ | |||
// TODO async resolving | |||
IPAddress ipAddress; | |||
bool parsed = IPAddress.TryParse("127.0.0.1", out ipAddress); | |||
IPEndPoint remoteEP = new IPEndPoint(ipAddress, targetPort); | |||
_remote = new Socket(ipAddress.AddressFamily, | |||
SocketType.Stream, ProtocolType.Tcp); | |||
_remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); | |||
// Connect to the remote endpoint. | |||
_remote.BeginConnect(remoteEP, | |||
new AsyncCallback(ConnectCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void ConnectCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndConnect(ar); | |||
HandshakeReceive(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void HandshakeReceive() | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.BeginSend(_firstPacket, 0, _firstPacketLength, 0, new AsyncCallback(StartPipe), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void StartPipe(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndSend(ar); | |||
_remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
_local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeRemoteReceiveCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
int bytesRead = _remote.EndReceive(ar); | |||
if (bytesRead > 0) | |||
{ | |||
_local.BeginSend(remoteRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null); | |||
} | |||
else | |||
{ | |||
//Console.WriteLine("bytesRead: " + bytesRead.ToString()); | |||
_local.Shutdown(SocketShutdown.Send); | |||
_localShutdown = true; | |||
CheckClose(); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeConnectionReceiveCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
int bytesRead = _local.EndReceive(ar); | |||
if (bytesRead > 0) | |||
{ | |||
_remote.BeginSend(connetionRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null); | |||
} | |||
else | |||
{ | |||
_remote.Shutdown(SocketShutdown.Send); | |||
_remoteShutdown = true; | |||
CheckClose(); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeRemoteSendCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndSend(ar); | |||
_local.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeConnectionSendCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_local.EndSend(ar); | |||
_remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void CheckClose() | |||
{ | |||
if (_localShutdown && _remoteShutdown) | |||
{ | |||
this.Close(); | |||
} | |||
} | |||
public void Close() | |||
{ | |||
lock (this) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
_closed = true; | |||
} | |||
if (_local != null) | |||
{ | |||
try | |||
{ | |||
_local.Shutdown(SocketShutdown.Both); | |||
_local.Close(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
if (_remote != null) | |||
{ | |||
try | |||
{ | |||
_remote.Shutdown(SocketShutdown.Both); | |||
_remote.Close(); | |||
} | |||
catch (SocketException e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class PortForwarder : Listener.Service | |||
{ | |||
int _targetPort; | |||
public PortForwarder(int targetPort) | |||
{ | |||
this._targetPort = targetPort; | |||
} | |||
public bool Handle(byte[] firstPacket, int length, Socket socket, object state) | |||
{ | |||
if (socket.ProtocolType != ProtocolType.Tcp) | |||
{ | |||
return false; | |||
} | |||
new Handler().Start(firstPacket, length, socket, this._targetPort); | |||
return true; | |||
} | |||
class Handler | |||
{ | |||
private byte[] _firstPacket; | |||
private int _firstPacketLength; | |||
private Socket _local; | |||
private Socket _remote; | |||
private bool _closed = false; | |||
private bool _localShutdown = false; | |||
private bool _remoteShutdown = false; | |||
public const int RecvSize = 16384; | |||
// remote receive buffer | |||
private byte[] remoteRecvBuffer = new byte[RecvSize]; | |||
// connection receive buffer | |||
private byte[] connetionRecvBuffer = new byte[RecvSize]; | |||
public void Start(byte[] firstPacket, int length, Socket socket, int targetPort) | |||
{ | |||
this._firstPacket = firstPacket; | |||
this._firstPacketLength = length; | |||
this._local = socket; | |||
try | |||
{ | |||
// TODO async resolving | |||
IPAddress ipAddress; | |||
bool parsed = IPAddress.TryParse("127.0.0.1", out ipAddress); | |||
IPEndPoint remoteEP = new IPEndPoint(ipAddress, targetPort); | |||
_remote = new Socket(ipAddress.AddressFamily, | |||
SocketType.Stream, ProtocolType.Tcp); | |||
_remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); | |||
// Connect to the remote endpoint. | |||
_remote.BeginConnect(remoteEP, | |||
new AsyncCallback(ConnectCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void ConnectCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndConnect(ar); | |||
HandshakeReceive(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void HandshakeReceive() | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.BeginSend(_firstPacket, 0, _firstPacketLength, 0, new AsyncCallback(StartPipe), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void StartPipe(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndSend(ar); | |||
_remote.BeginReceive(remoteRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
_local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeRemoteReceiveCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
int bytesRead = _remote.EndReceive(ar); | |||
if (bytesRead > 0) | |||
{ | |||
_local.BeginSend(remoteRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null); | |||
} | |||
else | |||
{ | |||
//Console.WriteLine("bytesRead: " + bytesRead.ToString()); | |||
_local.Shutdown(SocketShutdown.Send); | |||
_localShutdown = true; | |||
CheckClose(); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeConnectionReceiveCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
int bytesRead = _local.EndReceive(ar); | |||
if (bytesRead > 0) | |||
{ | |||
_remote.BeginSend(connetionRecvBuffer, 0, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null); | |||
} | |||
else | |||
{ | |||
_remote.Shutdown(SocketShutdown.Send); | |||
_remoteShutdown = true; | |||
CheckClose(); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeRemoteSendCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_remote.EndSend(ar); | |||
_local.BeginReceive(this.connetionRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void PipeConnectionSendCallback(IAsyncResult ar) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
_local.EndSend(ar); | |||
_remote.BeginReceive(this.remoteRecvBuffer, 0, RecvSize, 0, | |||
new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
this.Close(); | |||
} | |||
} | |||
private void CheckClose() | |||
{ | |||
if (_localShutdown && _remoteShutdown) | |||
{ | |||
this.Close(); | |||
} | |||
} | |||
public void Close() | |||
{ | |||
lock (this) | |||
{ | |||
if (_closed) | |||
{ | |||
return; | |||
} | |||
_closed = true; | |||
} | |||
if (_local != null) | |||
{ | |||
try | |||
{ | |||
_local.Shutdown(SocketShutdown.Both); | |||
_local.Close(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
if (_remote != null) | |||
{ | |||
try | |||
{ | |||
_remote.Shutdown(SocketShutdown.Both); | |||
_remote.Close(); | |||
} | |||
catch (SocketException e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,205 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using Shadowsocks.Encryption; | |||
using Shadowsocks.Model; | |||
using System.Net.Sockets; | |||
using System.Net; | |||
using System.Runtime.CompilerServices; | |||
using Shadowsocks.Controller.Strategy; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class UDPRelay : Listener.Service | |||
{ | |||
private ShadowsocksController _controller; | |||
private LRUCache<IPEndPoint, UDPHandler> _cache; | |||
public UDPRelay(ShadowsocksController controller) | |||
{ | |||
this._controller = controller; | |||
this._cache = new LRUCache<IPEndPoint, UDPHandler>(512); // todo: choose a smart number | |||
} | |||
public bool Handle(byte[] firstPacket, int length, Socket socket, object state) | |||
{ | |||
if (socket.ProtocolType != ProtocolType.Udp) | |||
{ | |||
return false; | |||
} | |||
if (length < 4) | |||
{ | |||
return false; | |||
} | |||
Listener.UDPState udpState = (Listener.UDPState)state; | |||
IPEndPoint remoteEndPoint = (IPEndPoint)udpState.remoteEndPoint; | |||
UDPHandler handler = _cache.get(remoteEndPoint); | |||
if (handler == null) | |||
{ | |||
handler = new UDPHandler(socket, _controller.GetAServer(IStrategyCallerType.UDP, remoteEndPoint), remoteEndPoint); | |||
_cache.add(remoteEndPoint, handler); | |||
} | |||
handler.Send(firstPacket, length); | |||
handler.Receive(); | |||
return true; | |||
} | |||
public class UDPHandler | |||
{ | |||
private Socket _local; | |||
private Socket _remote; | |||
private Server _server; | |||
private byte[] _buffer = new byte[1500]; | |||
private IPEndPoint _localEndPoint; | |||
private IPEndPoint _remoteEndPoint; | |||
public UDPHandler(Socket local, Server server, IPEndPoint localEndPoint) | |||
{ | |||
_local = local; | |||
_server = server; | |||
_localEndPoint = localEndPoint; | |||
// TODO async resolving | |||
IPAddress ipAddress; | |||
bool parsed = IPAddress.TryParse(server.server, out ipAddress); | |||
if (!parsed) | |||
{ | |||
IPHostEntry ipHostInfo = Dns.GetHostEntry(server.server); | |||
ipAddress = ipHostInfo.AddressList[0]; | |||
} | |||
_remoteEndPoint = new IPEndPoint(ipAddress, server.server_port); | |||
_remote = new Socket(_remoteEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp); | |||
} | |||
public void Send(byte[] data, int length) | |||
{ | |||
IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password); | |||
byte[] dataIn = new byte[length - 3]; | |||
Array.Copy(data, 3, dataIn, 0, length - 3); | |||
byte[] dataOut = new byte[length - 3 + 16]; | |||
int outlen; | |||
encryptor.Encrypt(dataIn, dataIn.Length, dataOut, out outlen); | |||
_remote.SendTo(dataOut, _remoteEndPoint); | |||
} | |||
public void Receive() | |||
{ | |||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | |||
_remote.BeginReceiveFrom(_buffer, 0, _buffer.Length, 0, ref remoteEndPoint, new AsyncCallback(RecvFromCallback), null); | |||
} | |||
public void RecvFromCallback(IAsyncResult ar) | |||
{ | |||
try | |||
{ | |||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | |||
int bytesRead = _remote.EndReceiveFrom(ar, ref remoteEndPoint); | |||
byte[] dataOut = new byte[bytesRead]; | |||
int outlen; | |||
IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password); | |||
encryptor.Decrypt(_buffer, bytesRead, dataOut, out outlen); | |||
byte[] sendBuf = new byte[outlen + 3]; | |||
Array.Copy(dataOut, 0, sendBuf, 3, outlen); | |||
_local.SendTo(sendBuf, outlen + 3, 0, _localEndPoint); | |||
Receive(); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
// TODO: handle the ObjectDisposedException | |||
} | |||
catch (Exception) | |||
{ | |||
// TODO: need more think about handle other Exceptions, or should remove this catch(). | |||
} | |||
finally | |||
{ | |||
} | |||
} | |||
public void Close() | |||
{ | |||
try | |||
{ | |||
_remote.Close(); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
// TODO: handle the ObjectDisposedException | |||
} | |||
catch (Exception) | |||
{ | |||
// TODO: need more think about handle other Exceptions, or should remove this catch(). | |||
} | |||
finally | |||
{ | |||
} | |||
} | |||
} | |||
} | |||
// cc by-sa 3.0 http://stackoverflow.com/a/3719378/1124054 | |||
class LRUCache<K, V> where V : UDPRelay.UDPHandler | |||
{ | |||
private int capacity; | |||
private Dictionary<K, LinkedListNode<LRUCacheItem<K, V>>> cacheMap = new Dictionary<K, LinkedListNode<LRUCacheItem<K, V>>>(); | |||
private LinkedList<LRUCacheItem<K, V>> lruList = new LinkedList<LRUCacheItem<K, V>>(); | |||
public LRUCache(int capacity) | |||
{ | |||
this.capacity = capacity; | |||
} | |||
[MethodImpl(MethodImplOptions.Synchronized)] | |||
public V get(K key) | |||
{ | |||
LinkedListNode<LRUCacheItem<K, V>> node; | |||
if (cacheMap.TryGetValue(key, out node)) | |||
{ | |||
V value = node.Value.value; | |||
lruList.Remove(node); | |||
lruList.AddLast(node); | |||
return value; | |||
} | |||
return default(V); | |||
} | |||
[MethodImpl(MethodImplOptions.Synchronized)] | |||
public void add(K key, V val) | |||
{ | |||
if (cacheMap.Count >= capacity) | |||
{ | |||
RemoveFirst(); | |||
} | |||
LRUCacheItem<K, V> cacheItem = new LRUCacheItem<K, V>(key, val); | |||
LinkedListNode<LRUCacheItem<K, V>> node = new LinkedListNode<LRUCacheItem<K, V>>(cacheItem); | |||
lruList.AddLast(node); | |||
cacheMap.Add(key, node); | |||
} | |||
private void RemoveFirst() | |||
{ | |||
// Remove from LRUPriority | |||
LinkedListNode<LRUCacheItem<K, V>> node = lruList.First; | |||
lruList.RemoveFirst(); | |||
// Remove from cache | |||
cacheMap.Remove(node.Value.key); | |||
node.Value.value.Close(); | |||
} | |||
} | |||
class LRUCacheItem<K, V> | |||
{ | |||
public LRUCacheItem(K k, V v) | |||
{ | |||
key = k; | |||
value = v; | |||
} | |||
public K key; | |||
public V value; | |||
} | |||
} |
@@ -1,159 +1,137 @@ | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Reflection; | |||
using System.Text; | |||
using System.Text.RegularExpressions; | |||
using System.Xml; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class UpdateChecker | |||
{ | |||
private const string UpdateURL = "https://sourceforge.net/api/file/index/project-id/1817190/path/dist/mtime/desc/limit/10/rss"; | |||
public string LatestVersionNumber; | |||
public string LatestVersionURL; | |||
public event EventHandler NewVersionFound; | |||
public const string Version = "2.3.1"; | |||
public void CheckUpdate(Configuration config) | |||
{ | |||
// TODO test failures | |||
WebClient http = new WebClient(); | |||
http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); | |||
http.DownloadStringCompleted += http_DownloadStringCompleted; | |||
http.DownloadStringAsync(new Uri(UpdateURL)); | |||
} | |||
public static int CompareVersion(string l, string r) | |||
{ | |||
var ls = l.Split('.'); | |||
var rs = r.Split('.'); | |||
for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++) | |||
{ | |||
int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0; | |||
int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0; | |||
if (lp != rp) | |||
{ | |||
return lp - rp; | |||
} | |||
} | |||
return 0; | |||
} | |||
public class VersionComparer : IComparer<string> | |||
{ | |||
// Calls CaseInsensitiveComparer.Compare with the parameters reversed. | |||
public int Compare(string x, string y) | |||
{ | |||
return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y)); | |||
} | |||
} | |||
private static string ParseVersionFromURL(string url) | |||
{ | |||
Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase); | |||
if (match.Success) | |||
{ | |||
if (match.Groups.Count == 2) | |||
{ | |||
return match.Groups[1].Value; | |||
} | |||
} | |||
return null; | |||
} | |||
private void SortVersions(List<string> versions) | |||
{ | |||
versions.Sort(new VersionComparer()); | |||
} | |||
private bool IsNewVersion(string url) | |||
{ | |||
if (url.IndexOf("prerelease") >= 0) | |||
{ | |||
return false; | |||
} | |||
// check dotnet 4.0 | |||
AssemblyName[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); | |||
Version dotNetVersion = Environment.Version; | |||
foreach (AssemblyName reference in references) | |||
{ | |||
if (reference.Name == "mscorlib") | |||
{ | |||
dotNetVersion = reference.Version; | |||
} | |||
} | |||
if (dotNetVersion.Major >= 4) | |||
{ | |||
if (url.IndexOf("dotnet4.0") < 0) | |||
{ | |||
return false; | |||
} | |||
} | |||
else | |||
{ | |||
if (url.IndexOf("dotnet4.0") >= 0) | |||
{ | |||
return false; | |||
} | |||
} | |||
string version = ParseVersionFromURL(url); | |||
if (version == null) | |||
{ | |||
return false; | |||
} | |||
string currentVersion = Version; | |||
return CompareVersion(version, currentVersion) > 0; | |||
} | |||
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) | |||
{ | |||
try | |||
{ | |||
string response = e.Result; | |||
XmlDocument xmlDoc = new XmlDocument(); | |||
xmlDoc.LoadXml(response); | |||
XmlNodeList elements = xmlDoc.GetElementsByTagName("media:content"); | |||
List<string> versions = new List<string>(); | |||
foreach (XmlNode el in elements) | |||
{ | |||
foreach (XmlAttribute attr in el.Attributes) | |||
{ | |||
if (attr.Name == "url") | |||
{ | |||
if (IsNewVersion(attr.Value)) | |||
{ | |||
versions.Add(attr.Value); | |||
} | |||
} | |||
} | |||
} | |||
if (versions.Count == 0) | |||
{ | |||
return; | |||
} | |||
// sort versions | |||
SortVersions(versions); | |||
LatestVersionURL = versions[versions.Count - 1]; | |||
LatestVersionNumber = ParseVersionFromURL(LatestVersionURL); | |||
if (NewVersionFound != null) | |||
{ | |||
NewVersionFound(this, new EventArgs()); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
Console.WriteLine(ex.ToString()); | |||
return; | |||
} | |||
} | |||
} | |||
} | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Reflection; | |||
using System.Text; | |||
using System.Text.RegularExpressions; | |||
using SimpleJson; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class UpdateChecker | |||
{ | |||
private const string UpdateURL = "https://api.github.com/repos/shadowsocks/shadowsocks-windows/releases"; | |||
public string LatestVersionNumber; | |||
public string LatestVersionURL; | |||
public event EventHandler NewVersionFound; | |||
public const string Version = "2.5.6"; | |||
public void CheckUpdate(Configuration config) | |||
{ | |||
// TODO test failures | |||
WebClient http = new WebClient(); | |||
http.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36"); | |||
http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); | |||
http.DownloadStringCompleted += http_DownloadStringCompleted; | |||
http.DownloadStringAsync(new Uri(UpdateURL)); | |||
} | |||
public static int CompareVersion(string l, string r) | |||
{ | |||
var ls = l.Split('.'); | |||
var rs = r.Split('.'); | |||
for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++) | |||
{ | |||
int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0; | |||
int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0; | |||
if (lp != rp) | |||
{ | |||
return lp - rp; | |||
} | |||
} | |||
return 0; | |||
} | |||
public class VersionComparer : IComparer<string> | |||
{ | |||
// Calls CaseInsensitiveComparer.Compare with the parameters reversed. | |||
public int Compare(string x, string y) | |||
{ | |||
return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y)); | |||
} | |||
} | |||
private static string ParseVersionFromURL(string url) | |||
{ | |||
Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase); | |||
if (match.Success) | |||
{ | |||
if (match.Groups.Count == 2) | |||
{ | |||
return match.Groups[1].Value; | |||
} | |||
} | |||
return null; | |||
} | |||
private void SortVersions(List<string> versions) | |||
{ | |||
versions.Sort(new VersionComparer()); | |||
} | |||
private bool IsNewVersion(string url) | |||
{ | |||
if (url.IndexOf("prerelease") >= 0) | |||
{ | |||
return false; | |||
} | |||
string version = ParseVersionFromURL(url); | |||
if (version == null) | |||
{ | |||
return false; | |||
} | |||
string currentVersion = Version; | |||
return CompareVersion(version, currentVersion) > 0; | |||
} | |||
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) | |||
{ | |||
try | |||
{ | |||
string response = e.Result; | |||
JsonArray result = (JsonArray)SimpleJson.SimpleJson.DeserializeObject(e.Result); | |||
List<string> versions = new List<string>(); | |||
foreach (JsonObject release in result) | |||
{ | |||
if ((bool)release["prerelease"]) | |||
{ | |||
continue; | |||
} | |||
foreach (JsonObject asset in (JsonArray)release["assets"]) | |||
{ | |||
string url = (string)asset["browser_download_url"]; | |||
if (IsNewVersion(url)) | |||
{ | |||
versions.Add(url); | |||
} | |||
} | |||
} | |||
if (versions.Count == 0) | |||
{ | |||
return; | |||
} | |||
// sort versions | |||
SortVersions(versions); | |||
LatestVersionURL = versions[versions.Count - 1]; | |||
LatestVersionNumber = ParseVersionFromURL(LatestVersionURL); | |||
if (NewVersionFound != null) | |||
{ | |||
NewVersionFound(this, new EventArgs()); | |||
} | |||
} | |||
catch (Exception ex) | |||
{ | |||
Logging.Debug(ex.ToString()); | |||
return; | |||
} | |||
} | |||
} | |||
} |
@@ -5,6 +5,8 @@ using System.Collections.Generic; | |||
using System.Text; | |||
using System.Threading; | |||
using System.Net.Sockets; | |||
using Shadowsocks.Controller.Strategy; | |||
using System.Net; | |||
namespace Shadowsocks.Controller | |||
{ | |||
@@ -20,8 +22,10 @@ namespace Shadowsocks.Controller | |||
private Listener _listener; | |||
private PACServer _pacServer; | |||
private Configuration _config; | |||
private StrategyManager _strategyManager; | |||
private PolipoRunner polipoRunner; | |||
private GFWListUpdater gfwListUpdater; | |||
private AvailabilityStatistics _availabilityStatics; | |||
private bool stopped = false; | |||
private bool _systemProxyIsDirty = false; | |||
@@ -49,6 +53,8 @@ namespace Shadowsocks.Controller | |||
public ShadowsocksController() | |||
{ | |||
_config = Configuration.Load(); | |||
_strategyManager = new StrategyManager(this); | |||
StartReleasingMemory(); | |||
} | |||
public void Start() | |||
@@ -70,11 +76,48 @@ namespace Shadowsocks.Controller | |||
} | |||
// always return copy | |||
public Configuration GetConfiguration() | |||
public Configuration GetConfigurationCopy() | |||
{ | |||
return Configuration.Load(); | |||
} | |||
// always return current instance | |||
public Configuration GetCurrentConfiguration() | |||
{ | |||
return _config; | |||
} | |||
public IList<IStrategy> GetStrategies() | |||
{ | |||
return _strategyManager.GetStrategies(); | |||
} | |||
public IStrategy GetCurrentStrategy() | |||
{ | |||
foreach (var strategy in _strategyManager.GetStrategies()) | |||
{ | |||
if (strategy.ID == this._config.strategy) | |||
{ | |||
return strategy; | |||
} | |||
} | |||
return null; | |||
} | |||
public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint) | |||
{ | |||
IStrategy strategy = GetCurrentStrategy(); | |||
if (strategy != null) | |||
{ | |||
return strategy.GetAServer(type, localIPEndPoint); | |||
} | |||
if (_config.index < 0) | |||
{ | |||
_config.index = 0; | |||
} | |||
return GetCurrentServer(); | |||
} | |||
public void SaveServers(List<Server> servers, int localPort) | |||
{ | |||
_config.configs = servers; | |||
@@ -134,36 +177,15 @@ namespace Shadowsocks.Controller | |||
public void SelectServerIndex(int index) | |||
{ | |||
_config.index = index; | |||
_config.strategy = null; | |||
SaveConfig(_config); | |||
} | |||
public void SelectServerIndexTemp(int index) | |||
public void SelectStrategy(string strategyID) | |||
{ | |||
_config.index = index; | |||
if (polipoRunner == null) | |||
polipoRunner = new PolipoRunner(); | |||
if (_listener != null) | |||
_listener.Stop(); | |||
try | |||
{ | |||
Local local = new Local(_config); | |||
List<Listener.Service> services = new List<Listener.Service>(); | |||
services.Add(local); | |||
_listener = new Listener(services); | |||
_listener.Start(_config); | |||
} | |||
catch (Exception e) | |||
{ | |||
var se = e as SocketException; | |||
if (se?.SocketErrorCode == SocketError.AccessDenied) | |||
e = new Exception(I18N.GetString("Port already in use"), e); | |||
Logging.LogUsefulException(e); | |||
ReportError(e); | |||
} | |||
Util.Utils.ReleaseMemory(); | |||
_config.index = -1; | |||
_config.strategy = strategyID; | |||
SaveConfig(_config); | |||
} | |||
public void Stop() | |||
@@ -208,6 +230,11 @@ namespace Shadowsocks.Controller | |||
public string GetQRCodeForCurrentServer() | |||
{ | |||
Server server = GetCurrentServer(); | |||
return GetQRCode(server); | |||
} | |||
public static string GetQRCode(Server server) | |||
{ | |||
string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port; | |||
string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts)); | |||
return "ss://" + base64; | |||
@@ -221,6 +248,16 @@ namespace Shadowsocks.Controller | |||
} | |||
} | |||
public void ToggleAvailabilityStatistics(bool enabled) | |||
{ | |||
if (_availabilityStatics != null) | |||
{ | |||
_availabilityStatics.Set(enabled); | |||
_config.availabilityStatistics = enabled; | |||
SaveConfig(_config); | |||
} | |||
} | |||
public void SavePACUrl(string pacUrl) | |||
{ | |||
_config.pacUrl = pacUrl; | |||
@@ -270,6 +307,12 @@ namespace Shadowsocks.Controller | |||
_listener.Stop(); | |||
} | |||
if (_availabilityStatics == null) | |||
{ | |||
_availabilityStatics = new AvailabilityStatistics(); | |||
_availabilityStatics.UpdateConfiguration(_config); | |||
} | |||
// don't put polipoRunner.Start() before pacServer.Stop() | |||
// or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1 | |||
// though UseShellExecute is set to true now | |||
@@ -277,11 +320,19 @@ namespace Shadowsocks.Controller | |||
polipoRunner.Stop(); | |||
try | |||
{ | |||
var strategy = GetCurrentStrategy(); | |||
if (strategy != null) | |||
{ | |||
strategy.ReloadServers(); | |||
} | |||
polipoRunner.Start(_config); | |||
Local local = new Local(_config); | |||
TCPRelay tcpRelay = new TCPRelay(this); | |||
UDPRelay udpRelay = new UDPRelay(this); | |||
List<Listener.Service> services = new List<Listener.Service>(); | |||
services.Add(local); | |||
services.Add(tcpRelay); | |||
services.Add(udpRelay); | |||
services.Add(_pacServer); | |||
services.Add(new PortForwarder(polipoRunner.RunningPort)); | |||
_listener = new Listener(services); | |||
@@ -309,17 +360,15 @@ namespace Shadowsocks.Controller | |||
} | |||
UpdateSystemProxy(); | |||
Util.Utils.ReleaseMemory(); | |||
Util.Utils.ReleaseMemory(true); | |||
} | |||
protected void SaveConfig(Configuration newConfig) | |||
{ | |||
Configuration.Save(newConfig); | |||
Reload(); | |||
} | |||
private void UpdateSystemProxy() | |||
{ | |||
if (_config.enabled) | |||
@@ -366,7 +415,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
while (true) | |||
{ | |||
Util.Utils.ReleaseMemory(); | |||
Util.Utils.ReleaseMemory(false); | |||
Thread.Sleep(30 * 1000); | |||
} | |||
} | |||
@@ -0,0 +1,71 @@ | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Text; | |||
namespace Shadowsocks.Controller.Strategy | |||
{ | |||
class BalancingStrategy : IStrategy | |||
{ | |||
ShadowsocksController _controller; | |||
Random _random; | |||
public BalancingStrategy(ShadowsocksController controller) | |||
{ | |||
_controller = controller; | |||
_random = new Random(); | |||
} | |||
public string Name | |||
{ | |||
get { return I18N.GetString("Load Balance"); } | |||
} | |||
public string ID | |||
{ | |||
get { return "com.shadowsocks.strategy.balancing"; } | |||
} | |||
public void ReloadServers() | |||
{ | |||
// do nothing | |||
} | |||
public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint) | |||
{ | |||
var configs = _controller.GetCurrentConfiguration().configs; | |||
int index; | |||
if (type == IStrategyCallerType.TCP) | |||
{ | |||
index = _random.Next(); | |||
} | |||
else | |||
{ | |||
index = localIPEndPoint.GetHashCode(); | |||
} | |||
return configs[index % configs.Count]; | |||
} | |||
public void UpdateLatency(Model.Server server, TimeSpan latency) | |||
{ | |||
// do nothing | |||
} | |||
public void UpdateLastRead(Model.Server server) | |||
{ | |||
// do nothing | |||
} | |||
public void UpdateLastWrite(Model.Server server) | |||
{ | |||
// do nothing | |||
} | |||
public void SetFailure(Model.Server server) | |||
{ | |||
// do nothing | |||
} | |||
} | |||
} |
@@ -0,0 +1,185 @@ | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace Shadowsocks.Controller.Strategy | |||
{ | |||
class HighAvailabilityStrategy : IStrategy | |||
{ | |||
protected ServerStatus _currentServer; | |||
protected Dictionary<Server, ServerStatus> _serverStatus; | |||
ShadowsocksController _controller; | |||
Random _random; | |||
public class ServerStatus | |||
{ | |||
// time interval between SYN and SYN+ACK | |||
public TimeSpan latency; | |||
public DateTime lastTimeDetectLatency; | |||
// last time anything received | |||
public DateTime lastRead; | |||
// last time anything sent | |||
public DateTime lastWrite; | |||
// connection refused or closed before anything received | |||
public DateTime lastFailure; | |||
public Server server; | |||
public double score; | |||
} | |||
public HighAvailabilityStrategy(ShadowsocksController controller) | |||
{ | |||
_controller = controller; | |||
_random = new Random(); | |||
_serverStatus = new Dictionary<Server, ServerStatus>(); | |||
} | |||
public string Name | |||
{ | |||
get { return I18N.GetString("High Availability"); } | |||
} | |||
public string ID | |||
{ | |||
get { return "com.shadowsocks.strategy.ha"; } | |||
} | |||
public void ReloadServers() | |||
{ | |||
// make a copy to avoid locking | |||
var newServerStatus = new Dictionary<Server, ServerStatus>(_serverStatus); | |||
foreach (var server in _controller.GetCurrentConfiguration().configs) | |||
{ | |||
if (!newServerStatus.ContainsKey(server)) | |||
{ | |||
var status = new ServerStatus(); | |||
status.server = server; | |||
status.lastFailure = DateTime.MinValue; | |||
status.lastRead = DateTime.Now; | |||
status.lastWrite = DateTime.Now; | |||
status.latency = new TimeSpan(0, 0, 0, 0, 10); | |||
status.lastTimeDetectLatency = DateTime.Now; | |||
newServerStatus[server] = status; | |||
} | |||
else | |||
{ | |||
// update settings for existing server | |||
newServerStatus[server].server = server; | |||
} | |||
} | |||
_serverStatus = newServerStatus; | |||
ChooseNewServer(); | |||
} | |||
public Server GetAServer(IStrategyCallerType type, System.Net.IPEndPoint localIPEndPoint) | |||
{ | |||
if (type == IStrategyCallerType.TCP) | |||
{ | |||
ChooseNewServer(); | |||
} | |||
if (_currentServer == null) | |||
{ | |||
return null; | |||
} | |||
return _currentServer.server; | |||
} | |||
/** | |||
* once failed, try after 5 min | |||
* and (last write - last read) < 5s | |||
* and (now - last read) < 5s // means not stuck | |||
* and latency < 200ms, try after 30s | |||
*/ | |||
public void ChooseNewServer() | |||
{ | |||
ServerStatus oldServer = _currentServer; | |||
List<ServerStatus> servers = new List<ServerStatus>(_serverStatus.Values); | |||
DateTime now = DateTime.Now; | |||
foreach (var status in servers) | |||
{ | |||
// all of failure, latency, (lastread - lastwrite) normalized to 1000, then | |||
// 100 * failure - 2 * latency - 0.5 * (lastread - lastwrite) | |||
status.score = | |||
100 * 1000 * Math.Min(5 * 60, (now - status.lastFailure).TotalSeconds) | |||
-2 * 5 * (Math.Min(2000, status.latency.TotalMilliseconds) / (1 + (now - status.lastTimeDetectLatency).TotalSeconds / 30 / 10) + | |||
-0.5 * 200 * Math.Min(5, (status.lastRead - status.lastWrite).TotalSeconds)); | |||
Logging.Debug(String.Format("server: {0} latency:{1} score: {2}", status.server.FriendlyName(), status.latency, status.score)); | |||
} | |||
ServerStatus max = null; | |||
foreach (var status in servers) | |||
{ | |||
if (max == null) | |||
{ | |||
max = status; | |||
} | |||
else | |||
{ | |||
if (status.score >= max.score) | |||
{ | |||
max = status; | |||
} | |||
} | |||
} | |||
if (max != null) | |||
{ | |||
if (_currentServer == null || max.score - _currentServer.score > 200) | |||
{ | |||
_currentServer = max; | |||
Console.WriteLine("HA switching to server: {0}", _currentServer.server.FriendlyName()); | |||
} | |||
} | |||
} | |||
public void UpdateLatency(Model.Server server, TimeSpan latency) | |||
{ | |||
Logging.Debug(String.Format("latency: {0} {1}", server.FriendlyName(), latency)); | |||
ServerStatus status; | |||
if (_serverStatus.TryGetValue(server, out status)) | |||
{ | |||
status.latency = latency; | |||
status.lastTimeDetectLatency = DateTime.Now; | |||
} | |||
} | |||
public void UpdateLastRead(Model.Server server) | |||
{ | |||
Logging.Debug(String.Format("last read: {0}", server.FriendlyName())); | |||
ServerStatus status; | |||
if (_serverStatus.TryGetValue(server, out status)) | |||
{ | |||
status.lastRead = DateTime.Now; | |||
} | |||
} | |||
public void UpdateLastWrite(Model.Server server) | |||
{ | |||
Logging.Debug(String.Format("last write: {0}", server.FriendlyName())); | |||
ServerStatus status; | |||
if (_serverStatus.TryGetValue(server, out status)) | |||
{ | |||
status.lastWrite = DateTime.Now; | |||
} | |||
} | |||
public void SetFailure(Model.Server server) | |||
{ | |||
Logging.Debug(String.Format("failure: {0}", server.FriendlyName())); | |||
ServerStatus status; | |||
if (_serverStatus.TryGetValue(server, out status)) | |||
{ | |||
status.lastFailure = DateTime.Now; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,56 @@ | |||
using Shadowsocks.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Net; | |||
using System.Text; | |||
namespace Shadowsocks.Controller.Strategy | |||
{ | |||
public enum IStrategyCallerType | |||
{ | |||
TCP, | |||
UDP | |||
} | |||
/* | |||
* IStrategy | |||
* | |||
* Subclasses must be thread-safe | |||
*/ | |||
public interface IStrategy | |||
{ | |||
string Name { get; } | |||
string ID { get; } | |||
/* | |||
* Called when servers need to be reloaded, i.e. new configuration saved | |||
*/ | |||
void ReloadServers(); | |||
/* | |||
* Get a new server to use in TCPRelay or UDPRelay | |||
*/ | |||
Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint); | |||
/* | |||
* TCPRelay will call this when latency of a server detected | |||
*/ | |||
void UpdateLatency(Server server, TimeSpan latency); | |||
/* | |||
* TCPRelay will call this when reading from a server | |||
*/ | |||
void UpdateLastRead(Server server); | |||
/* | |||
* TCPRelay will call this when writing to a server | |||
*/ | |||
void UpdateLastWrite(Server server); | |||
/* | |||
* TCPRelay will call this when fatal failure detected | |||
*/ | |||
void SetFailure(Server server); | |||
} | |||
} |
@@ -0,0 +1,176 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Net; | |||
using System.Text; | |||
using Shadowsocks.Model; | |||
using System.IO; | |||
using System.Net.NetworkInformation; | |||
using System.Threading; | |||
namespace Shadowsocks.Controller.Strategy | |||
{ | |||
class SimplyChooseByStatisticsStrategy : IStrategy | |||
{ | |||
private ShadowsocksController _controller; | |||
private Server _currentServer; | |||
private Timer timer; | |||
private Dictionary<string, StatisticsData> statistics; | |||
private static readonly int CachedInterval = 30 * 60 * 1000; //choose a new server every 30 minutes | |||
public SimplyChooseByStatisticsStrategy(ShadowsocksController controller) | |||
{ | |||
_controller = controller; | |||
var servers = controller.GetCurrentConfiguration().configs; | |||
int randomIndex = new Random().Next() % servers.Count(); | |||
_currentServer = servers[randomIndex]; //choose a server randomly at first | |||
timer = new Timer(ReloadStatisticsAndChooseAServer); | |||
} | |||
private void ReloadStatisticsAndChooseAServer(object obj) | |||
{ | |||
Logging.Debug("Reloading statistics and choose a new server...."); | |||
List<Server> servers = _controller.GetCurrentConfiguration().configs; | |||
LoadStatistics(); | |||
ChooseNewServer(servers); | |||
} | |||
/* | |||
return a dict: | |||
{ | |||
'ServerFriendlyName1':StatisticsData, | |||
'ServerFriendlyName2':... | |||
} | |||
*/ | |||
private void LoadStatistics() | |||
{ | |||
try | |||
{ | |||
var path = AvailabilityStatistics.AvailabilityStatisticsFile; | |||
Logging.Debug(string.Format("loading statistics from{0}", path)); | |||
statistics = (from l in File.ReadAllLines(path) | |||
.Skip(1) | |||
let strings = l.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries) | |||
let rawData = new | |||
{ | |||
ServerName = strings[1], | |||
IPStatus = strings[2], | |||
RoundtripTime = int.Parse(strings[3]) | |||
} | |||
group rawData by rawData.ServerName into server | |||
select new | |||
{ | |||
ServerName = server.Key, | |||
data = new StatisticsData | |||
{ | |||
SuccessTimes = server.Count(data => IPStatus.Success.ToString().Equals(data.IPStatus)), | |||
TimedOutTimes = server.Count(data => IPStatus.TimedOut.ToString().Equals(data.IPStatus)), | |||
AverageResponse = Convert.ToInt32(server.Average(data => data.RoundtripTime)), | |||
MinResponse = server.Min(data => data.RoundtripTime), | |||
MaxResponse = server.Max(data => data.RoundtripTime) | |||
} | |||
}).ToDictionary(server => server.ServerName, server => server.data); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
//return the score by data | |||
//server with highest score will be choosen | |||
private static double GetScore(StatisticsData data) | |||
{ | |||
return (double)data.SuccessTimes / (data.SuccessTimes + data.TimedOutTimes); //simply choose min package loss | |||
} | |||
private class StatisticsData | |||
{ | |||
public int SuccessTimes; | |||
public int TimedOutTimes; | |||
public int AverageResponse; | |||
public int MinResponse; | |||
public int MaxResponse; | |||
} | |||
private void ChooseNewServer(List<Server> servers) | |||
{ | |||
if (statistics == null) | |||
{ | |||
return; | |||
} | |||
try | |||
{ | |||
var bestResult = (from server in servers | |||
let name = server.FriendlyName() | |||
where statistics.ContainsKey(name) | |||
select new | |||
{ | |||
server, | |||
score = GetScore(statistics[name]) | |||
} | |||
).Aggregate((result1, result2) => result1.score > result2.score ? result1 : result2); | |||
if (_controller.GetCurrentStrategy().ID == ID && _currentServer != bestResult.server) //output when enabled | |||
{ | |||
Console.WriteLine("Switch to server: {0} by package loss:{1}", bestResult.server.FriendlyName(), 1 - bestResult.score); | |||
} | |||
_currentServer = bestResult.server; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
public string ID | |||
{ | |||
get { return "com.shadowsocks.strategy.scbs"; } | |||
} | |||
public string Name | |||
{ | |||
get { return I18N.GetString("Choose By Total Package Loss"); } | |||
} | |||
public Server GetAServer(IStrategyCallerType type, IPEndPoint localIPEndPoint) | |||
{ | |||
var oldServer = _currentServer; | |||
if (oldServer == null) | |||
{ | |||
ChooseNewServer(_controller.GetCurrentConfiguration().configs); | |||
} | |||
if (oldServer != _currentServer) | |||
{ | |||
} | |||
return _currentServer; //current server cached for CachedInterval | |||
} | |||
public void ReloadServers() | |||
{ | |||
ChooseNewServer(_controller.GetCurrentConfiguration().configs); | |||
timer?.Change(0, CachedInterval); | |||
} | |||
public void SetFailure(Server server) | |||
{ | |||
Logging.Debug(String.Format("failure: {0}", server.FriendlyName())); | |||
} | |||
public void UpdateLastRead(Server server) | |||
{ | |||
//TODO: combine this part of data with ICMP statics | |||
} | |||
public void UpdateLastWrite(Server server) | |||
{ | |||
//TODO: combine this part of data with ICMP statics | |||
} | |||
public void UpdateLatency(Server server, TimeSpan latency) | |||
{ | |||
//TODO: combine this part of data with ICMP statics | |||
} | |||
} | |||
} |
@@ -0,0 +1,24 @@ | |||
using Shadowsocks.Controller; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace Shadowsocks.Controller.Strategy | |||
{ | |||
class StrategyManager | |||
{ | |||
List<IStrategy> _strategies; | |||
public StrategyManager(ShadowsocksController controller) | |||
{ | |||
_strategies = new List<IStrategy>(); | |||
_strategies.Add(new BalancingStrategy(controller)); | |||
_strategies.Add(new HighAvailabilityStrategy(controller)); | |||
_strategies.Add(new SimplyChooseByStatisticsStrategy(controller)); | |||
// TODO: load DLL plugins | |||
} | |||
public IList<IStrategy> GetStrategies() | |||
{ | |||
return _strategies; | |||
} | |||
} | |||
} |
@@ -1,55 +1,55 @@ | |||
using System; | |||
using System.Windows.Forms; | |||
using Microsoft.Win32; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class AutoStartup | |||
{ | |||
public static bool Set(bool enabled) | |||
{ | |||
try | |||
{ | |||
string path = Application.ExecutablePath; | |||
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); | |||
if (enabled) | |||
{ | |||
runKey.SetValue("Shadowsocks", path); | |||
} | |||
else | |||
{ | |||
runKey.DeleteValue("Shadowsocks"); | |||
} | |||
runKey.Close(); | |||
return true; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
return false; | |||
} | |||
} | |||
public static bool Check() | |||
{ | |||
try | |||
{ | |||
string path = Application.ExecutablePath; | |||
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); | |||
string[] runList = runKey.GetValueNames(); | |||
runKey.Close(); | |||
foreach (string item in runList) | |||
{ | |||
if (item.Equals("Shadowsocks")) | |||
return true; | |||
} | |||
return false; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
return false; | |||
} | |||
} | |||
} | |||
} | |||
using System; | |||
using System.Windows.Forms; | |||
using Microsoft.Win32; | |||
namespace Shadowsocks.Controller | |||
{ | |||
class AutoStartup | |||
{ | |||
public static bool Set(bool enabled) | |||
{ | |||
try | |||
{ | |||
string path = Application.ExecutablePath; | |||
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); | |||
if (enabled) | |||
{ | |||
runKey.SetValue("Shadowsocks", path); | |||
} | |||
else | |||
{ | |||
runKey.DeleteValue("Shadowsocks"); | |||
} | |||
runKey.Close(); | |||
return true; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
return false; | |||
} | |||
} | |||
public static bool Check() | |||
{ | |||
try | |||
{ | |||
string path = Application.ExecutablePath; | |||
RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); | |||
string[] runList = runKey.GetValueNames(); | |||
runKey.Close(); | |||
foreach (string item in runList) | |||
{ | |||
if (item.Equals("Shadowsocks")) | |||
return true; | |||
} | |||
return false; | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
return false; | |||
} | |||
} | |||
} | |||
} |
@@ -1,110 +1,138 @@ | |||
using System.Windows.Forms; | |||
using Microsoft.Win32; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Runtime.InteropServices; | |||
using System.Text; | |||
using System.IO; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class SystemProxy | |||
{ | |||
[DllImport("wininet.dll")] | |||
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); | |||
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; | |||
public const int INTERNET_OPTION_REFRESH = 37; | |||
static bool _settingsReturn, _refreshReturn; | |||
public static void NotifyIE() | |||
{ | |||
// These lines implement the Interface in the beginning of program | |||
// They cause the OS to refresh the settings, causing IP to realy update | |||
_settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); | |||
_refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); | |||
} | |||
public static void Update(Configuration config, bool forceDisable) | |||
{ | |||
bool global = config.global; | |||
bool enabled = config.enabled; | |||
if (forceDisable) | |||
{ | |||
enabled = false; | |||
} | |||
try | |||
{ | |||
RegistryKey registry = | |||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", | |||
true); | |||
if (enabled) | |||
{ | |||
if (global) | |||
{ | |||
registry.SetValue("ProxyEnable", 1); | |||
registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString()); | |||
registry.SetValue("AutoConfigURL", ""); | |||
} | |||
else | |||
{ | |||
string pacUrl; | |||
if (config.useOnlinePac && !string.IsNullOrEmpty(config.pacUrl)) | |||
pacUrl = config.pacUrl; | |||
else | |||
pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now); | |||
registry.SetValue("ProxyEnable", 0); | |||
registry.SetValue("ProxyServer", ""); | |||
registry.SetValue("AutoConfigURL", pacUrl); | |||
} | |||
} | |||
else | |||
{ | |||
registry.SetValue("ProxyEnable", 0); | |||
registry.SetValue("ProxyServer", ""); | |||
registry.SetValue("AutoConfigURL", ""); | |||
} | |||
SystemProxy.NotifyIE(); | |||
//Must Notify IE first, or the connections do not chanage | |||
CopyProxySettingFromLan(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
// TODO this should be moved into views | |||
MessageBox.Show(I18N.GetString("Failed to update registry")); | |||
} | |||
} | |||
private static void CopyProxySettingFromLan() | |||
{ | |||
RegistryKey registry = | |||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", | |||
true); | |||
var defaultValue = registry.GetValue("DefaultConnectionSettings"); | |||
try | |||
{ | |||
var connections = registry.GetValueNames(); | |||
foreach (String each in connections) | |||
{ | |||
if (!(each.Equals("DefaultConnectionSettings") | |||
|| each.Equals("LAN Connection") | |||
|| each.Equals("SavedLegacySettings"))) | |||
{ | |||
//set all the connections's proxy as the lan | |||
registry.SetValue(each, defaultValue); | |||
} | |||
} | |||
SystemProxy.NotifyIE(); | |||
} catch (IOException e) { | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
private static String GetTimestamp(DateTime value) | |||
{ | |||
return value.ToString("yyyyMMddHHmmssffff"); | |||
} | |||
} | |||
} | |||
using System.Windows.Forms; | |||
using Microsoft.Win32; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Runtime.InteropServices; | |||
using System.Text; | |||
using System.IO; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.Controller | |||
{ | |||
public class SystemProxy | |||
{ | |||
[DllImport("wininet.dll")] | |||
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); | |||
public const int INTERNET_OPTION_SETTINGS_CHANGED = 39; | |||
public const int INTERNET_OPTION_REFRESH = 37; | |||
static bool _settingsReturn, _refreshReturn; | |||
public static void NotifyIE() | |||
{ | |||
// These lines implement the Interface in the beginning of program | |||
// They cause the OS to refresh the settings, causing IP to realy update | |||
_settingsReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); | |||
_refreshReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); | |||
} | |||
public static void Update(Configuration config, bool forceDisable) | |||
{ | |||
bool global = config.global; | |||
bool enabled = config.enabled; | |||
if (forceDisable) | |||
{ | |||
enabled = false; | |||
} | |||
try | |||
{ | |||
RegistryKey registry = | |||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", | |||
true); | |||
if (enabled) | |||
{ | |||
if (global) | |||
{ | |||
registry.SetValue("ProxyEnable", 1); | |||
registry.SetValue("ProxyServer", "127.0.0.1:" + config.localPort.ToString()); | |||
registry.SetValue("AutoConfigURL", ""); | |||
} | |||
else | |||
{ | |||
string pacUrl; | |||
if (config.useOnlinePac && !string.IsNullOrEmpty(config.pacUrl)) | |||
pacUrl = config.pacUrl; | |||
else | |||
pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?t=" + GetTimestamp(DateTime.Now); | |||
registry.SetValue("ProxyEnable", 0); | |||
var readProxyServer = registry.GetValue("ProxyServer"); | |||
registry.SetValue("ProxyServer", ""); | |||
registry.SetValue("AutoConfigURL", pacUrl); | |||
} | |||
} | |||
else | |||
{ | |||
registry.SetValue("ProxyEnable", 0); | |||
registry.SetValue("ProxyServer", ""); | |||
registry.SetValue("AutoConfigURL", ""); | |||
} | |||
//Set AutoDetectProxy Off | |||
IEAutoDetectProxy(false); | |||
SystemProxy.NotifyIE(); | |||
//Must Notify IE first, or the connections do not chanage | |||
CopyProxySettingFromLan(); | |||
} | |||
catch (Exception e) | |||
{ | |||
Logging.LogUsefulException(e); | |||
// TODO this should be moved into views | |||
MessageBox.Show(I18N.GetString("Failed to update registry")); | |||
} | |||
} | |||
private static void CopyProxySettingFromLan() | |||
{ | |||
RegistryKey registry = | |||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", | |||
true); | |||
var defaultValue = registry.GetValue("DefaultConnectionSettings"); | |||
try | |||
{ | |||
var connections = registry.GetValueNames(); | |||
foreach (String each in connections) | |||
{ | |||
if (!(each.Equals("DefaultConnectionSettings") | |||
|| each.Equals("LAN Connection") | |||
|| each.Equals("SavedLegacySettings"))) | |||
{ | |||
//set all the connections's proxy as the lan | |||
registry.SetValue(each, defaultValue); | |||
} | |||
} | |||
SystemProxy.NotifyIE(); | |||
} catch (IOException e) { | |||
Logging.LogUsefulException(e); | |||
} | |||
} | |||
private static String GetTimestamp(DateTime value) | |||
{ | |||
return value.ToString("yyyyMMddHHmmssffff"); | |||
} | |||
/// <summary> | |||
/// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy" | |||
/// </summary> | |||
/// <param name="set">Provide 'true' if you want to check the 'Automatically detect Proxy' check box. To uncheck, pass 'false'</param> | |||
private static void IEAutoDetectProxy(bool set) | |||
{ | |||
RegistryKey registry = | |||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", | |||
true); | |||
byte[] defConnection = (byte[])registry.GetValue("DefaultConnectionSettings"); | |||
byte[] savedLegacySetting = (byte[])registry.GetValue("SavedLegacySettings"); | |||
if (set) | |||
{ | |||
defConnection[8] = Convert.ToByte(defConnection[8] & 8); | |||
savedLegacySetting[8] = Convert.ToByte(savedLegacySetting[8] & 8); | |||
} | |||
else | |||
{ | |||
defConnection[8] = Convert.ToByte(defConnection[8] & ~8); | |||
savedLegacySetting[8] = Convert.ToByte(savedLegacySetting[8] & ~8); | |||
} | |||
registry.SetValue("DefaultConnectionSettings", defConnection); | |||
registry.SetValue("SavedLegacySettings", savedLegacySetting); | |||
} | |||
} | |||
} |
@@ -19,10 +19,14 @@ Update Local PAC from GFWList=从 GFWList 更新本地 PAC | |||
Edit User Rule for GFWList...=编辑 GFWList 的用户规则... | |||
Show QRCode...=显示二维码... | |||
Scan QRCode from Screen...=扫描屏幕上的二维码... | |||
Availability Statistics=统计可用性 | |||
Show Logs...=显示日志... | |||
About...=关于... | |||
Quit=退出 | |||
Edit Servers=编辑服务器 | |||
Load Balance=负载均衡 | |||
High Availability=高可用 | |||
Choose By Total Package Loss=累计丢包率 | |||
# Config Form | |||
@@ -38,6 +42,19 @@ Remarks=备注 | |||
OK=确定 | |||
Cancel=取消 | |||
New server=未配置的服务器 | |||
Move &Up=上移(&U) | |||
Move D&own=下移(&O) | |||
# Log Form | |||
&File=文件(&F) | |||
&Open Location=在资源管理器中打开(&O) | |||
E&xit=退出(&X) | |||
&Clean logs=清空(&C) | |||
&Font=字体(&F) | |||
&Wrap text=自动换行(&W) | |||
&Top most=置顶(&T) | |||
Log Viewer=日志查看器 | |||
# QRCode Form | |||
@@ -58,6 +75,7 @@ Please add at least one server=请添加至少一个服务器 | |||
Server IP can not be blank=服务器 IP 不能为空 | |||
Password can not be blank=密码不能为空 | |||
Port out of range=端口超出范围 | |||
Port can't be 8123=端口不能为 8123 | |||
Shadowsocks {0} Update Found=Shadowsocks {0} 更新 | |||
Click here to download=点击这里下载 | |||
Shadowsocks is here=Shadowsocks 在这里 | |||
@@ -68,29 +86,10 @@ Failed to update PAC file =更新 PAC 文件失败 | |||
PAC updated=更新 PAC 成功 | |||
No updates found. Please report to GFWList if you have problems with it.=未发现更新。如有问题请提交给 GFWList。 | |||
No QRCode found. Try to zoom in or move it to the center of the screen.=未发现二维码,尝试把它放大或移动到靠近屏幕中间的位置 | |||
Shadowsocks is already running.=Shadowsocks 已经在运行。 | |||
Find Shadowsocks icon in your notify tray.=请在任务栏里寻找 Shadowsocks 图标。 | |||
If you want to start multiple Shadowsocks, make a copy in another directory.=如果想同时启动多个,可以另外复制一份到别的目录。 | |||
Failed to decode QRCode=无法解析二维码 | |||
Failed to update registry=无法修改注册表 | |||
System Proxy On: =系统代理已启用: | |||
Running: Port {0}=正在运行:端口 {0} | |||
# Pingform | |||
UsableTest...=可用性测试... | |||
UsableTest=服务器可用性测试 | |||
SvcAddr=地址 | |||
Location=物理地址 | |||
Max=最大 | |||
Min=最小 | |||
Average=平均 | |||
IP=IP地址 | |||
Speed=下载速度 | |||
TestSpeed=测速 | |||
Pinging=正在获取 | |||
PingFail=获取失败 | |||
FailTime=失败次数 | |||
CurrentStatus:=当前状态: | |||
Ready=准备就绪 | |||
DoSomething=我们正在处理一些事情 | |||
Wrong=遇到一些问题 | |||
Unknow=未知 | |||
Seems your server is down, r u sure apply this server?=看起来你的服务器不可用,确定要使用这个服务器吗? |
@@ -1,10 +0,0 @@ | |||
proxyAddress = "__POLIPO_BIND_IP__" | |||
proxyPort = 8123 | |||
socksParentProxy = "127.0.0.1:__SOCKS_PORT__" | |||
socksProxyType = socks5 | |||
diskCacheRoot = "" | |||
localDocumentRoot = "" | |||
allowedPorts = 1-65535 | |||
tunnelAllowedPorts = 1-65535 |
@@ -0,0 +1,5 @@ | |||
listen-address __POLIPO_BIND_IP__:8123 | |||
show-on-task-bar 0 | |||
activity-animation 0 | |||
forward-socks5 / 127.0.0.1:__SOCKS_PORT__ . | |||
hide-console |
@@ -26,7 +26,6 @@ namespace Shadowsocks.Encryption | |||
protected int keyLen; | |||
protected int ivLen; | |||
public IVEncryptor(string method, string password) | |||
: base(method, password) | |||
{ | |||
@@ -1,5 +1,6 @@ | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks.Util; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
@@ -18,7 +19,7 @@ namespace Shadowsocks.Encryption | |||
static PolarSSL() | |||
{ | |||
string tempPath = Path.GetTempPath(); | |||
string tempPath = Utils.GetTempPath(); | |||
string dllPath = tempPath + "/libsscrypto.dll"; | |||
try | |||
{ | |||
@@ -49,7 +50,6 @@ namespace Shadowsocks.Encryption | |||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||
public extern static int aes_crypt_cfb128(IntPtr ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); | |||
public const int ARC4_CTX_SIZE = 264; | |||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||
@@ -1,5 +1,6 @@ | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks.Util; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
@@ -14,7 +15,7 @@ namespace Shadowsocks.Encryption | |||
static Sodium() | |||
{ | |||
string tempPath = Path.GetTempPath(); | |||
string tempPath = Utils.GetTempPath(); | |||
string dllPath = tempPath + "/libsscrypto.dll"; | |||
try | |||
{ | |||
@@ -1,6 +1,7 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
using System.Threading; | |||
namespace Shadowsocks.Encryption | |||
{ | |||
@@ -12,19 +13,17 @@ namespace Shadowsocks.Encryption | |||
const int SODIUM_BLOCK_SIZE = 64; | |||
static byte[] sodiumBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; | |||
protected int _encryptBytesRemaining; | |||
protected int _decryptBytesRemaining; | |||
protected ulong _encryptIC; | |||
protected ulong _decryptIC; | |||
protected byte[] _encryptBuf; | |||
protected byte[] _decryptBuf; | |||
public SodiumEncryptor(string method, string password) | |||
: base(method, password) | |||
{ | |||
InitKey(method, password); | |||
_encryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; | |||
_decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; | |||
} | |||
private static Dictionary<string, int[]> _ciphers = new Dictionary<string, int[]> { | |||
@@ -47,48 +46,51 @@ namespace Shadowsocks.Encryption | |||
// TODO write a unidirection cipher so we don't have to if if if | |||
int bytesRemaining; | |||
ulong ic; | |||
byte[] sodiumBuf; | |||
byte[] iv; | |||
if (isCipher) | |||
{ | |||
bytesRemaining = _encryptBytesRemaining; | |||
ic = _encryptIC; | |||
sodiumBuf = _encryptBuf; | |||
iv = _encryptIV; | |||
} | |||
else | |||
{ | |||
bytesRemaining = _decryptBytesRemaining; | |||
ic = _decryptIC; | |||
sodiumBuf = _decryptBuf; | |||
iv = _decryptIV; | |||
} | |||
int padding = bytesRemaining; | |||
Buffer.BlockCopy(buf, 0, sodiumBuf, padding, length); | |||
switch (_cipher) | |||
// I'm tired. just add a big lock | |||
// let's optimize for RAM instead of CPU | |||
lock(sodiumBuf) | |||
{ | |||
case CIPHER_SALSA20: | |||
Sodium.crypto_stream_salsa20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key); | |||
break; | |||
case CIPHER_CHACHA20: | |||
Sodium.crypto_stream_chacha20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key); | |||
break; | |||
} | |||
Buffer.BlockCopy(sodiumBuf, padding, outbuf, 0, length); | |||
padding += length; | |||
ic += (ulong)padding / SODIUM_BLOCK_SIZE; | |||
bytesRemaining = padding % SODIUM_BLOCK_SIZE; | |||
if (isCipher) | |||
{ | |||
bytesRemaining = _encryptBytesRemaining; | |||
ic = _encryptIC; | |||
iv = _encryptIV; | |||
} | |||
else | |||
{ | |||
bytesRemaining = _decryptBytesRemaining; | |||
ic = _decryptIC; | |||
iv = _decryptIV; | |||
} | |||
int padding = bytesRemaining; | |||
Buffer.BlockCopy(buf, 0, sodiumBuf, padding, length); | |||
if (isCipher) | |||
{ | |||
_encryptBytesRemaining = bytesRemaining; | |||
_encryptIC = ic; | |||
} | |||
else | |||
{ | |||
_decryptBytesRemaining = bytesRemaining; | |||
_decryptIC = ic; | |||
switch (_cipher) | |||
{ | |||
case CIPHER_SALSA20: | |||
Sodium.crypto_stream_salsa20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key); | |||
break; | |||
case CIPHER_CHACHA20: | |||
Sodium.crypto_stream_chacha20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key); | |||
break; | |||
} | |||
Buffer.BlockCopy(sodiumBuf, padding, outbuf, 0, length); | |||
padding += length; | |||
ic += (ulong)padding / SODIUM_BLOCK_SIZE; | |||
bytesRemaining = padding % SODIUM_BLOCK_SIZE; | |||
if (isCipher) | |||
{ | |||
_encryptBytesRemaining = bytesRemaining; | |||
_encryptIC = ic; | |||
} | |||
else | |||
{ | |||
_decryptBytesRemaining = bytesRemaining; | |||
_decryptIC = ic; | |||
} | |||
} | |||
} | |||
@@ -41,7 +41,6 @@ namespace Shadowsocks.Encryption | |||
outlength = length; | |||
} | |||
public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) | |||
{ | |||
byte[] result = new byte[length]; | |||
@@ -100,7 +99,6 @@ namespace Shadowsocks.Encryption | |||
return sorted; | |||
} | |||
public override void Dispose() | |||
{ | |||
} | |||
@@ -11,6 +11,9 @@ namespace Shadowsocks.Model | |||
public class Configuration | |||
{ | |||
public List<Server> configs; | |||
// when strategy is set, index is ignored | |||
public string strategy; | |||
public int index; | |||
public bool global; | |||
public bool enabled; | |||
@@ -19,6 +22,7 @@ namespace Shadowsocks.Model | |||
public int localPort; | |||
public string pacUrl; | |||
public bool useOnlinePac; | |||
public bool availabilityStatistics; | |||
private static string CONFIG_FILE = "gui-config.json"; | |||
@@ -52,6 +56,13 @@ namespace Shadowsocks.Model | |||
{ | |||
config.localPort = 1080; | |||
} | |||
if (config.index == -1) | |||
{ | |||
if (config.strategy == null) | |||
{ | |||
config.index = 0; | |||
} | |||
} | |||
return config; | |||
} | |||
catch (Exception e) | |||
@@ -79,9 +90,16 @@ namespace Shadowsocks.Model | |||
{ | |||
config.index = config.configs.Count - 1; | |||
} | |||
if (config.index < 0) | |||
if (config.index < -1) | |||
{ | |||
config.index = -1; | |||
} | |||
if (config.index == -1) | |||
{ | |||
config.index = 0; | |||
if (config.strategy == null) | |||
{ | |||
config.index = 0; | |||
} | |||
} | |||
config.isDefault = false; | |||
try | |||
@@ -120,6 +138,15 @@ namespace Shadowsocks.Model | |||
} | |||
} | |||
public static void CheckLocalPort(int port) | |||
{ | |||
CheckPort(port); | |||
if (port == 8123) | |||
{ | |||
throw new ArgumentException(I18N.GetString("Port can't be 8123")); | |||
} | |||
} | |||
private static void CheckPassword(string password) | |||
{ | |||
if (string.IsNullOrEmpty(password)) | |||
@@ -18,6 +18,17 @@ namespace Shadowsocks.Model | |||
public string method; | |||
public string remarks; | |||
public override int GetHashCode() | |||
{ | |||
return server.GetHashCode() ^ server_port; | |||
} | |||
public override bool Equals(object obj) | |||
{ | |||
Server o2 = (Server)obj; | |||
return this.server == o2.server && this.server_port == o2.server_port; | |||
} | |||
public string FriendlyName() | |||
{ | |||
if (string.IsNullOrEmpty(server)) | |||
@@ -18,8 +18,8 @@ namespace Shadowsocks | |||
[STAThread] | |||
static void Main() | |||
{ | |||
Util.Utils.ReleaseMemory(); | |||
using (Mutex mutex = new Mutex(false, "Global\\" + "71981632-A427-497F-AB91-241CD227EC1F")) | |||
Util.Utils.ReleaseMemory(true); | |||
using (Mutex mutex = new Mutex(false, "Global\\Shadowsocks_" + Application.StartupPath.GetHashCode())) | |||
{ | |||
Application.EnableVisualStyles(); | |||
Application.SetCompatibleTextRenderingDefault(false); | |||
@@ -31,7 +31,9 @@ namespace Shadowsocks | |||
{ | |||
Process oldProcess = oldProcesses[0]; | |||
} | |||
MessageBox.Show("Shadowsocks is already running.\n\nFind Shadowsocks icon in your notify tray."); | |||
MessageBox.Show(I18N.GetString("Find Shadowsocks icon in your notify tray.") + "\n" + | |||
I18N.GetString("If you want to start multiple Shadowsocks, make a copy in another directory."), | |||
I18N.GetString("Shadowsocks is already running.")); | |||
return; | |||
} | |||
Directory.SetCurrentDirectory(Application.StartupPath); | |||
@@ -1,7 +1,7 @@ | |||
//------------------------------------------------------------------------------ | |||
// <auto-generated> | |||
// This code was generated by a tool. | |||
// Runtime Version:4.0.30319.18408 | |||
// Runtime Version:4.0.30319.42000 | |||
// | |||
// Changes to this file may cause incorrect behavior and will be lost if | |||
// the code is regenerated. | |||
@@ -113,29 +113,34 @@ namespace Shadowsocks.Properties { | |||
} | |||
/// <summary> | |||
/// Looks up a localized string similar to proxyAddress = "__POLIPO_BIND_IP__" | |||
///proxyPort = 8123 | |||
/// | |||
///socksParentProxy = "127.0.0.1:__SOCKS_PORT__" | |||
///socksProxyType = socks5 | |||
///diskCacheRoot = "" | |||
///localDocumentRoot = "" | |||
/// | |||
///allowedPorts = 1-65535 | |||
///tunnelAllowedPorts = 1-65535. | |||
/// Looks up a localized resource of type System.Byte[]. | |||
/// </summary> | |||
internal static byte[] mgwz_dll { | |||
get { | |||
object obj = ResourceManager.GetObject("mgwz_dll", resourceCulture); | |||
return ((byte[])(obj)); | |||
} | |||
} | |||
/// <summary> | |||
/// Looks up a localized string similar to listen-address __POLIPO_BIND_IP__:8123 | |||
///show-on-task-bar 0 | |||
///activity-animation 0 | |||
///forward-socks5 / 127.0.0.1:__SOCKS_PORT__ . | |||
///hide-console. | |||
/// </summary> | |||
internal static string polipo_config { | |||
internal static string privoxy_conf { | |||
get { | |||
return ResourceManager.GetString("polipo_config", resourceCulture); | |||
return ResourceManager.GetString("privoxy_conf", resourceCulture); | |||
} | |||
} | |||
/// <summary> | |||
/// Looks up a localized resource of type System.Byte[]. | |||
/// </summary> | |||
internal static byte[] polipo_exe { | |||
internal static byte[] privoxy_exe { | |||
get { | |||
object obj = ResourceManager.GetObject("polipo_exe", resourceCulture); | |||
object obj = ResourceManager.GetObject("privoxy_exe", resourceCulture); | |||
return ((byte[])(obj)); | |||
} | |||
} | |||
@@ -112,12 +112,12 @@ | |||
<value>2.0</value> | |||
</resheader> | |||
<resheader name="reader"> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<resheader name="writer"> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> | |||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> | |||
<data name="abp_js" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\Data\abp.js.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</data> | |||
@@ -127,11 +127,14 @@ | |||
<data name="libsscrypto_dll" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\data\libsscrypto.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</data> | |||
<data name="polipo_config" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\Data\polipo_config.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;gb2312</value> | |||
<data name="mgwz_dll" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\data\mgwz.dll.gz;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</data> | |||
<data name="polipo_exe" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\Data\polipo.exe.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<data name="privoxy_conf" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\data\privoxy_conf.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value> | |||
</data> | |||
<data name="privoxy_exe" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\data\privoxy.exe.gz;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</data> | |||
<data name="proxy_pac_txt" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||
<value>..\Data\proxy.pac.txt.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
@@ -1,27 +1,35 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
using System.Drawing; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Linq; | |||
using System.Runtime.InteropServices; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
using Shadowsocks._3rd.ProxySocket; | |||
namespace Shadowsocks.Util | |||
{ | |||
public class Utils | |||
{ | |||
public static Shadowsocks._3rd.QQWry qqwry; | |||
public static Font GetFont() | |||
// return path to store temporary files | |||
public static string GetTempPath() | |||
{ | |||
var fs = FontFamily.Families; | |||
var ret = fs.FirstOrDefault(f => f.Name == "Microsoft YaHei UI Light") ?? fs.FirstOrDefault(f => f.Name == "Microsoft YaHei UI"); | |||
return ret == null ? SystemFonts.MessageBoxFont : new Font(ret,9f,FontStyle.Regular); | |||
if (File.Exists(Application.StartupPath + "\\shadowsocks_portable_mode.txt")) | |||
{ | |||
try | |||
{ | |||
Directory.CreateDirectory(Application.StartupPath + "\\temp"); | |||
} catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
} | |||
// don't use "/", it will fail when we call explorer /select xxx/temp\xxx.log | |||
return Application.StartupPath + "\\temp"; | |||
} | |||
return Path.GetTempPath(); | |||
} | |||
public static void ReleaseMemory() | |||
public static void ReleaseMemory(bool removePages) | |||
{ | |||
// release any unused pages | |||
// making the numbers look good in task manager | |||
@@ -31,8 +39,29 @@ namespace Shadowsocks.Util | |||
// which is part of user experience | |||
GC.Collect(GC.MaxGeneration); | |||
GC.WaitForPendingFinalizers(); | |||
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, | |||
(UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); | |||
if (removePages) | |||
{ | |||
// as some users have pointed out | |||
// removing pages from working set will cause some IO | |||
// which lowered user experience for another group of users | |||
// | |||
// so we do 2 more things here to satisfy them: | |||
// 1. only remove pages once when configuration is changed | |||
// 2. add more comments here to tell users that calling | |||
// this function will not be more frequent than | |||
// IM apps writing chat logs, or web browsers writing cache files | |||
// if they're so concerned about their disk, they should | |||
// uninstall all IM apps and web browsers | |||
// | |||
// please open an issue if you're worried about anything else in your computer | |||
// no matter it's GPU performance, monitor contrast, audio fidelity | |||
// or anything else in the task manager | |||
// we'll do as much as we can to help you | |||
// | |||
// just kidding | |||
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, | |||
(UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); | |||
} | |||
} | |||
public static string UnGzip(byte[] buf) | |||
@@ -47,6 +47,9 @@ | |||
this.ServerGroupBox = new System.Windows.Forms.GroupBox(); | |||
this.ServersListBox = new System.Windows.Forms.ListBox(); | |||
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); | |||
this.tableLayoutPanel6 = new System.Windows.Forms.TableLayoutPanel(); | |||
this.MoveDownButton = new System.Windows.Forms.Button(); | |||
this.MoveUpButton = new System.Windows.Forms.Button(); | |||
this.tableLayoutPanel5 = new System.Windows.Forms.TableLayoutPanel(); | |||
this.ProxyPortTextBox = new System.Windows.Forms.TextBox(); | |||
this.ProxyPortLabel = new System.Windows.Forms.Label(); | |||
@@ -55,6 +58,7 @@ | |||
this.tableLayoutPanel1.SuspendLayout(); | |||
this.ServerGroupBox.SuspendLayout(); | |||
this.tableLayoutPanel2.SuspendLayout(); | |||
this.tableLayoutPanel6.SuspendLayout(); | |||
this.tableLayoutPanel5.SuspendLayout(); | |||
this.tableLayoutPanel3.SuspendLayout(); | |||
this.tableLayoutPanel4.SuspendLayout(); | |||
@@ -184,7 +188,7 @@ | |||
// | |||
// EncryptionSelect | |||
// | |||
this.EncryptionSelect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |||
this.EncryptionSelect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |||
| System.Windows.Forms.AnchorStyles.Right))); | |||
this.EncryptionSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; | |||
this.EncryptionSelect.FormattingEnabled = true; | |||
@@ -281,6 +285,7 @@ | |||
// | |||
this.ServersListBox.FormattingEnabled = true; | |||
this.ServersListBox.IntegralHeight = false; | |||
this.ServersListBox.ItemHeight = 12; | |||
this.ServersListBox.Location = new System.Drawing.Point(0, 0); | |||
this.ServersListBox.Margin = new System.Windows.Forms.Padding(0); | |||
this.ServersListBox.Name = "ServersListBox"; | |||
@@ -295,6 +300,7 @@ | |||
this.tableLayoutPanel2.ColumnCount = 2; | |||
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); | |||
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); | |||
this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel6, 0, 2); | |||
this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel5, 1, 1); | |||
this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 1, 2); | |||
this.tableLayoutPanel2.Controls.Add(this.ServersListBox, 0, 0); | |||
@@ -310,6 +316,48 @@ | |||
this.tableLayoutPanel2.Size = new System.Drawing.Size(427, 238); | |||
this.tableLayoutPanel2.TabIndex = 7; | |||
// | |||
// tableLayoutPanel6 | |||
// | |||
this.tableLayoutPanel6.AutoSize = true; | |||
this.tableLayoutPanel6.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; | |||
this.tableLayoutPanel6.ColumnCount = 2; | |||
this.tableLayoutPanel6.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); | |||
this.tableLayoutPanel6.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); | |||
this.tableLayoutPanel6.Controls.Add(this.MoveDownButton, 1, 0); | |||
this.tableLayoutPanel6.Controls.Add(this.MoveUpButton, 0, 0); | |||
this.tableLayoutPanel6.Dock = System.Windows.Forms.DockStyle.Top; | |||
this.tableLayoutPanel6.Location = new System.Drawing.Point(0, 211); | |||
this.tableLayoutPanel6.Margin = new System.Windows.Forms.Padding(0); | |||
this.tableLayoutPanel6.Name = "tableLayoutPanel6"; | |||
this.tableLayoutPanel6.RowCount = 1; | |||
this.tableLayoutPanel6.RowStyles.Add(new System.Windows.Forms.RowStyle()); | |||
this.tableLayoutPanel6.Size = new System.Drawing.Size(166, 32); | |||
this.tableLayoutPanel6.TabIndex = 10; | |||
// | |||
// MoveDownButton | |||
// | |||
this.MoveDownButton.Dock = System.Windows.Forms.DockStyle.Right; | |||
this.MoveDownButton.Location = new System.Drawing.Point(86, 6); | |||
this.MoveDownButton.Margin = new System.Windows.Forms.Padding(3, 6, 0, 3); | |||
this.MoveDownButton.Name = "MoveDownButton"; | |||
this.MoveDownButton.Size = new System.Drawing.Size(80, 23); | |||
this.MoveDownButton.TabIndex = 7; | |||
this.MoveDownButton.Text = "Move D&own"; | |||
this.MoveDownButton.UseVisualStyleBackColor = true; | |||
this.MoveDownButton.Click += new System.EventHandler(this.MoveDownButton_Click); | |||
// | |||
// MoveUpButton | |||
// | |||
this.MoveUpButton.Dock = System.Windows.Forms.DockStyle.Left; | |||
this.MoveUpButton.Location = new System.Drawing.Point(0, 6); | |||
this.MoveUpButton.Margin = new System.Windows.Forms.Padding(0, 6, 3, 3); | |||
this.MoveUpButton.Name = "MoveUpButton"; | |||
this.MoveUpButton.Size = new System.Drawing.Size(80, 23); | |||
this.MoveUpButton.TabIndex = 6; | |||
this.MoveUpButton.Text = "Move &Up"; | |||
this.MoveUpButton.UseVisualStyleBackColor = true; | |||
this.MoveUpButton.Click += new System.EventHandler(this.MoveUpButton_Click); | |||
// | |||
// tableLayoutPanel5 | |||
// | |||
this.tableLayoutPanel5.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | |||
@@ -418,6 +466,7 @@ | |||
this.ServerGroupBox.PerformLayout(); | |||
this.tableLayoutPanel2.ResumeLayout(false); | |||
this.tableLayoutPanel2.PerformLayout(); | |||
this.tableLayoutPanel6.ResumeLayout(false); | |||
this.tableLayoutPanel5.ResumeLayout(false); | |||
this.tableLayoutPanel5.PerformLayout(); | |||
this.tableLayoutPanel3.ResumeLayout(false); | |||
@@ -453,6 +502,9 @@ | |||
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel5; | |||
private System.Windows.Forms.TextBox ProxyPortTextBox; | |||
private System.Windows.Forms.Label ProxyPortLabel; | |||
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel6; | |||
private System.Windows.Forms.Button MoveDownButton; | |||
private System.Windows.Forms.Button MoveUpButton; | |||
} | |||
} | |||
@@ -18,16 +18,15 @@ namespace Shadowsocks.View | |||
// this is a copy of configuration that we are working on | |||
private Configuration _modifiedConfiguration; | |||
private int _oldSelectedIndex = -1; | |||
private int _lastSelectedIndex = -1; | |||
public ConfigForm(ShadowsocksController controller) | |||
{ | |||
//this.Font = System.Drawing.SystemFonts.MessageBoxFont; | |||
Font = Util.Utils.GetFont(); | |||
this.Font = System.Drawing.SystemFonts.MessageBoxFont; | |||
InitializeComponent(); | |||
// a dirty hack | |||
this.ServersListBox.Dock = DockStyle.Fill; | |||
this.ServersListBox.Dock = System.Windows.Forms.DockStyle.Fill; | |||
this.PerformLayout(); | |||
UpdateTexts(); | |||
@@ -52,6 +51,8 @@ namespace Shadowsocks.View | |||
ServerGroupBox.Text = I18N.GetString("Server"); | |||
OKButton.Text = I18N.GetString("OK"); | |||
MyCancelButton.Text = I18N.GetString("Cancel"); | |||
MoveUpButton.Text = I18N.GetString("Move &Up"); | |||
MoveDownButton.Text = I18N.GetString("Move D&own"); | |||
this.Text = I18N.GetString("Edit Servers"); | |||
} | |||
@@ -59,7 +60,7 @@ namespace Shadowsocks.View | |||
{ | |||
LoadCurrentConfiguration(); | |||
} | |||
private void ShowWindow() | |||
{ | |||
this.Opacity = 1; | |||
@@ -71,7 +72,7 @@ namespace Shadowsocks.View | |||
{ | |||
try | |||
{ | |||
if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) | |||
if (_lastSelectedIndex == -1 || _lastSelectedIndex >= _modifiedConfiguration.configs.Count) | |||
{ | |||
return true; | |||
} | |||
@@ -85,10 +86,10 @@ namespace Shadowsocks.View | |||
}; | |||
int localPort = int.Parse(ProxyPortTextBox.Text); | |||
Configuration.CheckServer(server); | |||
Configuration.CheckPort(localPort); | |||
_modifiedConfiguration.configs[_oldSelectedIndex] = server; | |||
Configuration.CheckLocalPort(localPort); | |||
_modifiedConfiguration.configs[_lastSelectedIndex] = server; | |||
_modifiedConfiguration.localPort = localPort; | |||
return true; | |||
} | |||
catch (FormatException) | |||
@@ -114,12 +115,6 @@ namespace Shadowsocks.View | |||
ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString(); | |||
EncryptionSelect.Text = server.method ?? "aes-256-cfb"; | |||
RemarksTextBox.Text = server.remarks; | |||
ServerGroupBox.Visible = true; | |||
//IPTextBox.Focus(); | |||
} | |||
else | |||
{ | |||
ServerGroupBox.Visible = false; | |||
} | |||
} | |||
@@ -134,10 +129,15 @@ namespace Shadowsocks.View | |||
private void LoadCurrentConfiguration() | |||
{ | |||
_modifiedConfiguration = controller.GetConfiguration(); | |||
_modifiedConfiguration = controller.GetConfigurationCopy(); | |||
LoadConfiguration(_modifiedConfiguration); | |||
_oldSelectedIndex = _modifiedConfiguration.index; | |||
ServersListBox.SelectedIndex = _modifiedConfiguration.index; | |||
_lastSelectedIndex = _modifiedConfiguration.index; | |||
if (_lastSelectedIndex < 0) | |||
{ | |||
_lastSelectedIndex = 0; | |||
} | |||
ServersListBox.SelectedIndex = _lastSelectedIndex; | |||
UpdateMoveUpAndDownButton(); | |||
LoadSelectedServer(); | |||
} | |||
@@ -148,7 +148,11 @@ namespace Shadowsocks.View | |||
private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) | |||
{ | |||
if (_oldSelectedIndex == ServersListBox.SelectedIndex) | |||
if (!ServersListBox.CanSelect) | |||
{ | |||
return; | |||
} | |||
if (_lastSelectedIndex == ServersListBox.SelectedIndex) | |||
{ | |||
// we are moving back to oldSelectedIndex or doing a force move | |||
return; | |||
@@ -156,11 +160,13 @@ namespace Shadowsocks.View | |||
if (!SaveOldSelectedServer()) | |||
{ | |||
// why this won't cause stack overflow? | |||
ServersListBox.SelectedIndex = _oldSelectedIndex; | |||
ServersListBox.SelectedIndex = _lastSelectedIndex; | |||
return; | |||
} | |||
ServersListBox.Items[_lastSelectedIndex] = _modifiedConfiguration.configs[_lastSelectedIndex].FriendlyName(); | |||
UpdateMoveUpAndDownButton(); | |||
LoadSelectedServer(); | |||
_oldSelectedIndex = ServersListBox.SelectedIndex; | |||
_lastSelectedIndex = ServersListBox.SelectedIndex; | |||
} | |||
private void AddButton_Click(object sender, EventArgs e) | |||
@@ -173,29 +179,30 @@ namespace Shadowsocks.View | |||
_modifiedConfiguration.configs.Add(server); | |||
LoadConfiguration(_modifiedConfiguration); | |||
ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1; | |||
_oldSelectedIndex = ServersListBox.SelectedIndex; | |||
_lastSelectedIndex = ServersListBox.SelectedIndex; | |||
} | |||
private void DeleteButton_Click(object sender, EventArgs e) | |||
{ | |||
_oldSelectedIndex = ServersListBox.SelectedIndex; | |||
if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count) | |||
_lastSelectedIndex = ServersListBox.SelectedIndex; | |||
if (_lastSelectedIndex >= 0 && _lastSelectedIndex < _modifiedConfiguration.configs.Count) | |||
{ | |||
_modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex); | |||
_modifiedConfiguration.configs.RemoveAt(_lastSelectedIndex); | |||
} | |||
if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count) | |||
if (_lastSelectedIndex >= _modifiedConfiguration.configs.Count) | |||
{ | |||
// can be -1 | |||
_oldSelectedIndex = _modifiedConfiguration.configs.Count - 1; | |||
_lastSelectedIndex = _modifiedConfiguration.configs.Count - 1; | |||
} | |||
ServersListBox.SelectedIndex = _oldSelectedIndex; | |||
ServersListBox.SelectedIndex = _lastSelectedIndex; | |||
LoadConfiguration(_modifiedConfiguration); | |||
ServersListBox.SelectedIndex = _oldSelectedIndex; | |||
ServersListBox.SelectedIndex = _lastSelectedIndex; | |||
LoadSelectedServer(); | |||
} | |||
private void OKButton_Click(object sender, EventArgs e) | |||
{ | |||
Server server = controller.GetCurrentServer(); | |||
if (!SaveOldSelectedServer()) | |||
{ | |||
return; | |||
@@ -206,6 +213,7 @@ namespace Shadowsocks.View | |||
return; | |||
} | |||
controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort); | |||
controller.SelectServerIndex(_modifiedConfiguration.configs.IndexOf(server)); | |||
this.Close(); | |||
} | |||
@@ -224,5 +232,70 @@ namespace Shadowsocks.View | |||
controller.ConfigChanged -= controller_ConfigChanged; | |||
} | |||
private void MoveConfigItem(int step) | |||
{ | |||
int index = ServersListBox.SelectedIndex; | |||
Server server = _modifiedConfiguration.configs[index]; | |||
object item = ServersListBox.SelectedItem; | |||
_modifiedConfiguration.configs.Remove(server); | |||
_modifiedConfiguration.configs.Insert(index + step, server); | |||
_modifiedConfiguration.index += step; | |||
ServersListBox.BeginUpdate(); | |||
ServersListBox.Enabled = false; | |||
_lastSelectedIndex = index + step; | |||
ServersListBox.Items.Remove(item); | |||
ServersListBox.Items.Insert(index + step, item); | |||
ServersListBox.Enabled = true; | |||
ServersListBox.SelectedIndex = index + step; | |||
ServersListBox.EndUpdate(); | |||
UpdateMoveUpAndDownButton(); | |||
} | |||
private void UpdateMoveUpAndDownButton() | |||
{ | |||
if (ServersListBox.SelectedIndex == 0) | |||
{ | |||
MoveUpButton.Enabled = false; | |||
} | |||
else | |||
{ | |||
MoveUpButton.Enabled = true; | |||
} | |||
if (ServersListBox.SelectedIndex == ServersListBox.Items.Count - 1) | |||
{ | |||
MoveDownButton.Enabled = false; | |||
} | |||
else | |||
{ | |||
MoveDownButton.Enabled = true; | |||
} | |||
} | |||
private void MoveUpButton_Click(object sender, EventArgs e) | |||
{ | |||
if (!SaveOldSelectedServer()) | |||
{ | |||
return; | |||
} | |||
if (ServersListBox.SelectedIndex > 0) | |||
{ | |||
MoveConfigItem(-1); // -1 means move backward | |||
} | |||
} | |||
private void MoveDownButton_Click(object sender, EventArgs e) | |||
{ | |||
if (!SaveOldSelectedServer()) | |||
{ | |||
return; | |||
} | |||
if (ServersListBox.SelectedIndex < ServersListBox.Items.Count - 1) | |||
{ | |||
MoveConfigItem(+1); // +1 means move forward | |||
} | |||
} | |||
} | |||
} |
@@ -112,9 +112,9 @@ | |||
<value>2.0</value> | |||
</resheader> | |||
<resheader name="reader"> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<resheader name="writer"> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
</root> |
@@ -0,0 +1,199 @@ | |||
namespace Shadowsocks.View | |||
{ | |||
partial class LogForm | |||
{ | |||
/// <summary> | |||
/// Required designer variable. | |||
/// </summary> | |||
private System.ComponentModel.IContainer components = null; | |||
/// <summary> | |||
/// Clean up any resources being used. | |||
/// </summary> | |||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |||
protected override void Dispose(bool disposing) | |||
{ | |||
if (disposing && (components != null)) | |||
{ | |||
components.Dispose(); | |||
} | |||
base.Dispose(disposing); | |||
} | |||
#region Windows Form Designer generated code | |||
/// <summary> | |||
/// Required method for Designer support - do not modify | |||
/// the contents of this method with the code editor. | |||
/// </summary> | |||
private void InitializeComponent() | |||
{ | |||
this.components = new System.ComponentModel.Container(); | |||
this.LogMessageTextBox = new System.Windows.Forms.TextBox(); | |||
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); | |||
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components); | |||
this.FileMenuItem = new System.Windows.Forms.MenuItem(); | |||
this.OpenLocationMenuItem = new System.Windows.Forms.MenuItem(); | |||
this.ExitMenuItem = new System.Windows.Forms.MenuItem(); | |||
this.panel1 = new System.Windows.Forms.Panel(); | |||
this.ChangeFontButton = new System.Windows.Forms.Button(); | |||
this.CleanLogsButton = new System.Windows.Forms.Button(); | |||
this.WrapTextCheckBox = new System.Windows.Forms.CheckBox(); | |||
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); | |||
this.TopMostCheckBox = new System.Windows.Forms.CheckBox(); | |||
this.panel1.SuspendLayout(); | |||
this.tableLayoutPanel1.SuspendLayout(); | |||
this.SuspendLayout(); | |||
// | |||
// LogMessageTextBox | |||
// | |||
this.LogMessageTextBox.BackColor = System.Drawing.Color.Black; | |||
this.LogMessageTextBox.Dock = System.Windows.Forms.DockStyle.Fill; | |||
this.LogMessageTextBox.Font = new System.Drawing.Font("Consolas", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); | |||
this.LogMessageTextBox.ForeColor = System.Drawing.Color.White; | |||
this.LogMessageTextBox.Location = new System.Drawing.Point(3, 43); | |||
this.LogMessageTextBox.MaxLength = 2147483647; | |||
this.LogMessageTextBox.Multiline = true; | |||
this.LogMessageTextBox.Name = "LogMessageTextBox"; | |||
this.LogMessageTextBox.ReadOnly = true; | |||
this.LogMessageTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both; | |||
this.LogMessageTextBox.Size = new System.Drawing.Size(541, 307); | |||
this.LogMessageTextBox.TabIndex = 0; | |||
this.LogMessageTextBox.WordWrap = false; | |||
// | |||
// contextMenuStrip1 | |||
// | |||
this.contextMenuStrip1.Name = "contextMenuStrip1"; | |||
this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4); | |||
// | |||
// mainMenu1 | |||
// | |||
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | |||
this.FileMenuItem}); | |||
// | |||
// FileMenuItem | |||
// | |||
this.FileMenuItem.Index = 0; | |||
this.FileMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | |||
this.OpenLocationMenuItem, | |||
this.ExitMenuItem}); | |||
this.FileMenuItem.Text = "&File"; | |||
// | |||
// OpenLocationMenuItem | |||
// | |||
this.OpenLocationMenuItem.Index = 0; | |||
this.OpenLocationMenuItem.Text = "&Open Location"; | |||
this.OpenLocationMenuItem.Click += new System.EventHandler(this.OpenLocationMenuItem_Click); | |||
// | |||
// ExitMenuItem | |||
// | |||
this.ExitMenuItem.Index = 1; | |||
this.ExitMenuItem.Text = "E&xit"; | |||
this.ExitMenuItem.Click += new System.EventHandler(this.ExitMenuItem_Click); | |||
// | |||
// panel1 | |||
// | |||
this.panel1.Controls.Add(this.TopMostCheckBox); | |||
this.panel1.Controls.Add(this.ChangeFontButton); | |||
this.panel1.Controls.Add(this.CleanLogsButton); | |||
this.panel1.Controls.Add(this.WrapTextCheckBox); | |||
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; | |||
this.panel1.Location = new System.Drawing.Point(3, 3); | |||
this.panel1.Name = "panel1"; | |||
this.panel1.Size = new System.Drawing.Size(541, 34); | |||
this.panel1.TabIndex = 1; | |||
// | |||
// ChangeFontButton | |||
// | |||
this.ChangeFontButton.Location = new System.Drawing.Point(107, 4); | |||
this.ChangeFontButton.Name = "ChangeFontButton"; | |||
this.ChangeFontButton.Size = new System.Drawing.Size(75, 23); | |||
this.ChangeFontButton.TabIndex = 2; | |||
this.ChangeFontButton.Text = "&Font"; | |||
this.ChangeFontButton.UseVisualStyleBackColor = true; | |||
this.ChangeFontButton.Click += new System.EventHandler(this.ChangeFontButton_Click); | |||
// | |||
// CleanLogsButton | |||
// | |||
this.CleanLogsButton.Location = new System.Drawing.Point(9, 4); | |||
this.CleanLogsButton.Name = "CleanLogsButton"; | |||
this.CleanLogsButton.Size = new System.Drawing.Size(75, 23); | |||
this.CleanLogsButton.TabIndex = 1; | |||
this.CleanLogsButton.Text = "&Clean logs"; | |||
this.CleanLogsButton.UseVisualStyleBackColor = true; | |||
this.CleanLogsButton.Click += new System.EventHandler(this.CleanLogsButton_Click); | |||
// | |||
// WrapTextCheckBox | |||
// | |||
this.WrapTextCheckBox.AutoSize = true; | |||
this.WrapTextCheckBox.Location = new System.Drawing.Point(209, 9); | |||
this.WrapTextCheckBox.Name = "WrapTextCheckBox"; | |||
this.WrapTextCheckBox.Size = new System.Drawing.Size(78, 16); | |||
this.WrapTextCheckBox.TabIndex = 0; | |||
this.WrapTextCheckBox.Text = "&Wrap text"; | |||
this.WrapTextCheckBox.UseVisualStyleBackColor = true; | |||
this.WrapTextCheckBox.CheckedChanged += new System.EventHandler(this.WrapTextCheckBox_CheckedChanged); | |||
// | |||
// tableLayoutPanel1 | |||
// | |||
this.tableLayoutPanel1.ColumnCount = 1; | |||
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); | |||
this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 0); | |||
this.tableLayoutPanel1.Controls.Add(this.LogMessageTextBox, 0, 1); | |||
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; | |||
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); | |||
this.tableLayoutPanel1.Name = "tableLayoutPanel1"; | |||
this.tableLayoutPanel1.RowCount = 2; | |||
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40F)); | |||
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); | |||
this.tableLayoutPanel1.Size = new System.Drawing.Size(547, 353); | |||
this.tableLayoutPanel1.TabIndex = 2; | |||
// | |||
// TopMostCheckBox | |||
// | |||
this.TopMostCheckBox.AutoSize = true; | |||
this.TopMostCheckBox.Location = new System.Drawing.Point(311, 9); | |||
this.TopMostCheckBox.Name = "TopMostCheckBox"; | |||
this.TopMostCheckBox.Size = new System.Drawing.Size(72, 16); | |||
this.TopMostCheckBox.TabIndex = 3; | |||
this.TopMostCheckBox.Text = "&Top most"; | |||
this.TopMostCheckBox.UseVisualStyleBackColor = true; | |||
this.TopMostCheckBox.CheckedChanged += new System.EventHandler(this.TopMostCheckBox_CheckedChanged); | |||
// | |||
// LogForm | |||
// | |||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); | |||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |||
this.ClientSize = new System.Drawing.Size(547, 353); | |||
this.Controls.Add(this.tableLayoutPanel1); | |||
this.Menu = this.mainMenu1; | |||
this.Name = "LogForm"; | |||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; | |||
this.Text = "Log Viewer"; | |||
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.LogForm_FormClosing); | |||
this.Load += new System.EventHandler(this.LogForm_Load); | |||
this.Shown += new System.EventHandler(this.LogForm_Shown); | |||
this.panel1.ResumeLayout(false); | |||
this.panel1.PerformLayout(); | |||
this.tableLayoutPanel1.ResumeLayout(false); | |||
this.tableLayoutPanel1.PerformLayout(); | |||
this.ResumeLayout(false); | |||
} | |||
#endregion | |||
private System.Windows.Forms.TextBox LogMessageTextBox; | |||
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; | |||
private System.Windows.Forms.MainMenu mainMenu1; | |||
private System.Windows.Forms.MenuItem FileMenuItem; | |||
private System.Windows.Forms.MenuItem OpenLocationMenuItem; | |||
private System.Windows.Forms.MenuItem ExitMenuItem; | |||
private System.Windows.Forms.Panel panel1; | |||
private System.Windows.Forms.CheckBox WrapTextCheckBox; | |||
private System.Windows.Forms.Button CleanLogsButton; | |||
private System.Windows.Forms.Button ChangeFontButton; | |||
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; | |||
private System.Windows.Forms.CheckBox TopMostCheckBox; | |||
} | |||
} |
@@ -0,0 +1,150 @@ | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Properties; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.Data; | |||
using System.Drawing; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
namespace Shadowsocks.View | |||
{ | |||
public partial class LogForm : Form | |||
{ | |||
long lastOffset; | |||
string filename; | |||
Timer timer; | |||
const int BACK_OFFSET = 65536; | |||
public LogForm(string filename) | |||
{ | |||
this.filename = filename; | |||
InitializeComponent(); | |||
this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); | |||
UpdateTexts(); | |||
} | |||
private void UpdateTexts() | |||
{ | |||
FileMenuItem.Text = I18N.GetString("&File"); | |||
OpenLocationMenuItem.Text = I18N.GetString("&Open Location"); | |||
ExitMenuItem.Text = I18N.GetString("E&xit"); | |||
CleanLogsButton.Text = I18N.GetString("&Clean logs"); | |||
ChangeFontButton.Text = I18N.GetString("&Font"); | |||
WrapTextCheckBox.Text = I18N.GetString("&Wrap text"); | |||
TopMostCheckBox.Text = I18N.GetString("&Top most"); | |||
this.Text = I18N.GetString("Log Viewer"); | |||
} | |||
private void Timer_Tick(object sender, EventArgs e) | |||
{ | |||
UpdateContent(); | |||
} | |||
private void InitContent() | |||
{ | |||
using (StreamReader reader = new StreamReader(new FileStream(filename, | |||
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) | |||
{ | |||
if (reader.BaseStream.Length > BACK_OFFSET) | |||
{ | |||
reader.BaseStream.Seek(-BACK_OFFSET, SeekOrigin.End); | |||
reader.ReadLine(); | |||
} | |||
string line = ""; | |||
while ((line = reader.ReadLine()) != null) | |||
LogMessageTextBox.AppendText(line + "\r\n"); | |||
LogMessageTextBox.ScrollToCaret(); | |||
lastOffset = reader.BaseStream.Position; | |||
} | |||
} | |||
private void UpdateContent() | |||
{ | |||
using (StreamReader reader = new StreamReader(new FileStream(filename, | |||
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) | |||
{ | |||
reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin); | |||
string line = ""; | |||
bool changed = false; | |||
while ((line = reader.ReadLine()) != null) | |||
{ | |||
changed = true; | |||
LogMessageTextBox.AppendText(line + "\r\n"); | |||
} | |||
if (changed) | |||
{ | |||
LogMessageTextBox.ScrollToCaret(); | |||
} | |||
lastOffset = reader.BaseStream.Position; | |||
} | |||
} | |||
private void LogForm_Load(object sender, EventArgs e) | |||
{ | |||
InitContent(); | |||
timer = new Timer(); | |||
timer.Interval = 300; | |||
timer.Tick += Timer_Tick; | |||
timer.Start(); | |||
} | |||
private void LogForm_FormClosing(object sender, FormClosingEventArgs e) | |||
{ | |||
timer.Stop(); | |||
} | |||
private void OpenLocationMenuItem_Click(object sender, EventArgs e) | |||
{ | |||
string argument = "/select, \"" + filename + "\""; | |||
Console.WriteLine(argument); | |||
System.Diagnostics.Process.Start("explorer.exe", argument); | |||
} | |||
private void ExitMenuItem_Click(object sender, EventArgs e) | |||
{ | |||
this.Close(); | |||
} | |||
private void LogForm_Shown(object sender, EventArgs e) | |||
{ | |||
LogMessageTextBox.ScrollToCaret(); | |||
} | |||
private void WrapTextCheckBox_CheckedChanged(object sender, EventArgs e) | |||
{ | |||
LogMessageTextBox.WordWrap = WrapTextCheckBox.Checked; | |||
LogMessageTextBox.ScrollToCaret(); | |||
} | |||
private void CleanLogsButton_Click(object sender, EventArgs e) | |||
{ | |||
LogMessageTextBox.Clear(); | |||
} | |||
private void ChangeFontButton_Click(object sender, EventArgs e) | |||
{ | |||
FontDialog fd = new FontDialog(); | |||
fd.Font = LogMessageTextBox.Font; | |||
if (fd.ShowDialog() == DialogResult.OK) | |||
{ | |||
LogMessageTextBox.Font = fd.Font; | |||
} | |||
} | |||
private void TopMostCheckBox_CheckedChanged(object sender, EventArgs e) | |||
{ | |||
this.TopMost = TopMostCheckBox.Checked; | |||
} | |||
} | |||
} |
@@ -117,31 +117,10 @@ | |||
<resheader name="writer"> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<metadata name="SvcAddr.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="IP.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="Max.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="Min.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="Average.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="FailTime.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="Speed.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="TestSpeed.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | |||
<value>True</value> | |||
</metadata> | |||
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | |||
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | |||
<value>17, 17</value> | |||
</metadata> | |||
<metadata name="mainMenu1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | |||
<value>172, 17</value> | |||
</metadata> | |||
</root> |
@@ -18,7 +18,7 @@ namespace Shadowsocks.View | |||
// yes this is just a menu view controller | |||
// when config form is closed, it moves away from RAM | |||
// and it should just do anything related to the config form | |||
private ShadowsocksController controller; | |||
private UpdateChecker updateChecker; | |||
@@ -29,6 +29,7 @@ namespace Shadowsocks.View | |||
private MenuItem enableItem; | |||
private MenuItem modeItem; | |||
private MenuItem AutoStartupItem; | |||
private MenuItem AvailabilityStatistics; | |||
private MenuItem ShareOverLANItem; | |||
private MenuItem SeperatorItem; | |||
private MenuItem ConfigItem; | |||
@@ -42,9 +43,6 @@ namespace Shadowsocks.View | |||
private MenuItem editGFWUserRuleItem; | |||
private MenuItem editOnlinePACItem; | |||
private ConfigForm configForm; | |||
private PingForm pingForm; | |||
private string _urlToOpen; | |||
public MenuViewController(ShadowsocksController controller) | |||
@@ -74,9 +72,9 @@ namespace Shadowsocks.View | |||
LoadCurrentConfiguration(); | |||
updateChecker.CheckUpdate(controller.GetConfiguration()); | |||
updateChecker.CheckUpdate(controller.GetConfigurationCopy()); | |||
if (controller.GetConfiguration().isDefault) | |||
if (controller.GetConfigurationCopy().isDefault) | |||
{ | |||
_isFirstRun = true; | |||
ShowConfigForm(); | |||
@@ -109,7 +107,7 @@ namespace Shadowsocks.View | |||
{ | |||
icon = Resources.ss24; | |||
} | |||
Configuration config = controller.GetConfiguration(); | |||
Configuration config = controller.GetConfigurationCopy(); | |||
bool enabled = config.enabled; | |||
bool global = config.global; | |||
if (!enabled) | |||
@@ -127,20 +125,29 @@ namespace Shadowsocks.View | |||
} | |||
_notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); | |||
string serverInfo = null; | |||
if (controller.GetCurrentStrategy() != null) | |||
{ | |||
serverInfo = controller.GetCurrentStrategy().Name; | |||
} | |||
else | |||
{ | |||
serverInfo = config.GetCurrentServer().FriendlyName(); | |||
} | |||
// we want to show more details but notify icon title is limited to 63 characters | |||
string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" + | |||
(enabled ? | |||
I18N.GetString("System Proxy On: ") + (global ? I18N.GetString("Global") : I18N.GetString("PAC")) : | |||
String.Format(I18N.GetString("Running: Port {0}"), config.localPort)) // this feedback is very important because they need to know Shadowsocks is running | |||
+ "\n" + config.GetCurrentServer().FriendlyName(); | |||
+ "\n" + serverInfo; | |||
_notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length)); | |||
} | |||
private MenuItem CreateMenuItem(string text, EventHandler click) | |||
{ | |||
return new MenuItem(I18N.GetString(text), click); | |||
} | |||
private MenuItem CreateMenuGroup(string text, MenuItem[] items) | |||
{ | |||
return new MenuItem(I18N.GetString(text), items); | |||
@@ -171,10 +178,9 @@ namespace Shadowsocks.View | |||
}), | |||
new MenuItem("-"), | |||
this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), | |||
this.AvailabilityStatistics = CreateMenuItem("Availability Statistics", new EventHandler(this.AvailabilityStatisticsItem_Click)), | |||
this.ShareOverLANItem = CreateMenuItem("Allow Clients from LAN", new EventHandler(this.ShareOverLANItem_Click)), | |||
new MenuItem("-"), | |||
CreateMenuItem("UsableTest...", new EventHandler(this.PingForm_Click)), | |||
new MenuItem("-"), | |||
CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)), | |||
CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)), | |||
new MenuItem("-"), | |||
@@ -190,18 +196,18 @@ namespace Shadowsocks.View | |||
private void controller_EnableStatusChanged(object sender, EventArgs e) | |||
{ | |||
enableItem.Checked = controller.GetConfiguration().enabled; | |||
enableItem.Checked = controller.GetConfigurationCopy().enabled; | |||
modeItem.Enabled = enableItem.Checked; | |||
} | |||
void controller_ShareOverLANStatusChanged(object sender, EventArgs e) | |||
{ | |||
ShareOverLANItem.Checked = controller.GetConfiguration().shareOverLan; | |||
ShareOverLANItem.Checked = controller.GetConfigurationCopy().shareOverLan; | |||
} | |||
void controller_EnableGlobalChanged(object sender, EventArgs e) | |||
{ | |||
globalModeItem.Checked = controller.GetConfiguration().global; | |||
globalModeItem.Checked = controller.GetConfigurationCopy().global; | |||
PACModeItem.Checked = !globalModeItem.Checked; | |||
} | |||
@@ -248,7 +254,7 @@ namespace Shadowsocks.View | |||
private void LoadCurrentConfiguration() | |||
{ | |||
Configuration config = controller.GetConfiguration(); | |||
Configuration config = controller.GetConfigurationCopy(); | |||
UpdateServersMenu(); | |||
enableItem.Checked = config.enabled; | |||
modeItem.Enabled = config.enabled; | |||
@@ -256,6 +262,7 @@ namespace Shadowsocks.View | |||
PACModeItem.Checked = !config.global; | |||
ShareOverLANItem.Checked = config.shareOverLan; | |||
AutoStartupItem.Checked = AutoStartup.Check(); | |||
AvailabilityStatistics.Checked = config.availabilityStatistics; | |||
onlinePACItem.Checked = onlinePACItem.Enabled && config.useOnlinePac; | |||
localPACItem.Checked = !onlinePACItem.Checked; | |||
UpdatePACItemsEnabledStatus(); | |||
@@ -268,44 +275,34 @@ namespace Shadowsocks.View | |||
{ | |||
items.RemoveAt(0); | |||
} | |||
Configuration configuration = controller.GetConfiguration(); | |||
for (int i = 0; i < configuration.configs.Count; i++) | |||
int i = 0; | |||
foreach (var strategy in controller.GetStrategies()) | |||
{ | |||
MenuItem item = new MenuItem(strategy.Name); | |||
item.Tag = strategy.ID; | |||
item.Click += AStrategyItem_Click; | |||
items.Add(i, item); | |||
i++; | |||
} | |||
int strategyCount = i; | |||
Configuration configuration = controller.GetConfigurationCopy(); | |||
foreach (var server in configuration.configs) | |||
{ | |||
Server server = configuration.configs[i]; | |||
MenuItem item = new MenuItem(server.FriendlyName()); | |||
item.Tag = i; | |||
item.Tag = i - strategyCount; | |||
item.Click += AServerItem_Click; | |||
items.Add(i, item); | |||
i++; | |||
} | |||
if (configuration.index >= 0 && configuration.index < configuration.configs.Count) | |||
foreach (MenuItem item in items) | |||
{ | |||
items[configuration.index].Checked = true; | |||
} | |||
} | |||
if (item.Tag != null && (item.Tag.ToString() == configuration.index.ToString() || item.Tag.ToString() == configuration.strategy)) | |||
{ | |||
item.Checked = true; | |||
} | |||
private void ShowPingForm() | |||
{ | |||
if (pingForm != null) | |||
{ | |||
pingForm.Activate(); | |||
} | |||
else | |||
{ | |||
pingForm = new PingForm(controller); | |||
pingForm.Show(); | |||
pingForm.FormClosed += pingForm_FormClosed; | |||
} | |||
} | |||
private void PingForm_Click(object sender, EventArgs e) | |||
{ | |||
ShowPingForm(); | |||
} | |||
void pingForm_FormClosed(object sender, FormClosedEventArgs e) | |||
{ | |||
pingForm = null; | |||
Util.Utils.ReleaseMemory(); | |||
} | |||
private void ShowConfigForm() | |||
@@ -325,7 +322,7 @@ namespace Shadowsocks.View | |||
void configForm_FormClosed(object sender, FormClosedEventArgs e) | |||
{ | |||
configForm = null; | |||
Util.Utils.ReleaseMemory(); | |||
Util.Utils.ReleaseMemory(true); | |||
ShowFirstTimeBalloon(); | |||
} | |||
@@ -355,7 +352,7 @@ namespace Shadowsocks.View | |||
private void AboutItem_Click(object sender, EventArgs e) | |||
{ | |||
Process.Start("https://github.com/shadowsocks/shadowsocks-csharp"); | |||
Process.Start("https://github.com/shadowsocks/shadowsocks-windows"); | |||
} | |||
private void notifyIcon1_DoubleClick(object sender, MouseEventArgs e) | |||
@@ -408,11 +405,17 @@ namespace Shadowsocks.View | |||
controller.SelectServerIndex((int)item.Tag); | |||
} | |||
private void AStrategyItem_Click(object sender, EventArgs e) | |||
{ | |||
MenuItem item = (MenuItem)sender; | |||
controller.SelectStrategy((string)item.Tag); | |||
} | |||
private void ShowLogItem_Click(object sender, EventArgs e) | |||
{ | |||
string argument = Logging.LogFile; | |||
System.Diagnostics.Process.Start("notepad.exe", argument); | |||
new LogForm(argument).Show(); | |||
} | |||
private void QRCodeItem_Click(object sender, EventArgs e) | |||
@@ -517,12 +520,17 @@ namespace Shadowsocks.View | |||
Process.Start(_urlToOpen); | |||
} | |||
private void AutoStartupItem_Click(object sender, EventArgs e) { | |||
AutoStartupItem.Checked = !AutoStartupItem.Checked; | |||
if (!AutoStartup.Set(AutoStartupItem.Checked)) { | |||
MessageBox.Show(I18N.GetString("Failed to update registry")); | |||
} | |||
} | |||
private void AutoStartupItem_Click(object sender, EventArgs e) { | |||
AutoStartupItem.Checked = !AutoStartupItem.Checked; | |||
if (!AutoStartup.Set(AutoStartupItem.Checked)) { | |||
MessageBox.Show(I18N.GetString("Failed to update registry")); | |||
} | |||
} | |||
private void AvailabilityStatisticsItem_Click(object sender, EventArgs e) { | |||
AvailabilityStatistics.Checked = !AvailabilityStatistics.Checked; | |||
controller.ToggleAvailabilityStatistics(AvailabilityStatistics.Checked); | |||
} | |||
private void LocalPACItem_Click(object sender, EventArgs e) | |||
{ | |||
@@ -539,11 +547,11 @@ namespace Shadowsocks.View | |||
{ | |||
if (!onlinePACItem.Checked) | |||
{ | |||
if (String.IsNullOrEmpty(controller.GetConfiguration().pacUrl)) | |||
if (String.IsNullOrEmpty(controller.GetConfigurationCopy().pacUrl)) | |||
{ | |||
UpdateOnlinePACURLItem_Click(sender, e); | |||
} | |||
if (!String.IsNullOrEmpty(controller.GetConfiguration().pacUrl)) | |||
if (!String.IsNullOrEmpty(controller.GetConfigurationCopy().pacUrl)) | |||
{ | |||
localPACItem.Checked = false; | |||
onlinePACItem.Checked = true; | |||
@@ -555,7 +563,7 @@ namespace Shadowsocks.View | |||
private void UpdateOnlinePACURLItem_Click(object sender, EventArgs e) | |||
{ | |||
string origPacUrl = controller.GetConfiguration().pacUrl; | |||
string origPacUrl = controller.GetConfigurationCopy().pacUrl; | |||
string pacUrl = Microsoft.VisualBasic.Interaction.InputBox( | |||
I18N.GetString("Please input PAC Url"), | |||
I18N.GetString("Edit Online PAC URL"), | |||
@@ -1,237 +0,0 @@ | |||
using Microsoft.VisualBasic; | |||
namespace Shadowsocks.View | |||
{ | |||
sealed partial class PingForm | |||
{ | |||
/// <summary> | |||
/// Required designer variable. | |||
/// </summary> | |||
private System.ComponentModel.IContainer components = null; | |||
/// <summary> | |||
/// Clean up any resources being used. | |||
/// </summary> | |||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |||
protected override void Dispose(bool disposing) | |||
{ | |||
if (disposing && (components != null)) | |||
{ | |||
components.Dispose(); | |||
} | |||
base.Dispose(disposing); | |||
} | |||
#region Windows Form Designer generated code | |||
/// <summary> | |||
/// Required method for Designer support - do not modify | |||
/// the contents of this method with the code editor. | |||
/// </summary> | |||
private void InitializeComponent() | |||
{ | |||
this.dgvMain = new System.Windows.Forms.DataGridView(); | |||
this.SvcAddr = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.IP = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Remarks = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Location = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Max = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Min = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Average = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.FailTime = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.Speed = new System.Windows.Forms.DataGridViewTextBoxColumn(); | |||
this.TestSpeed = new System.Windows.Forms.DataGridViewLinkColumn(); | |||
this.statusStrip1 = new System.Windows.Forms.StatusStrip(); | |||
this.tssStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); | |||
this.tssStatus = new System.Windows.Forms.ToolStripStatusLabel(); | |||
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); | |||
this.pBar = new System.Windows.Forms.ToolStripProgressBar(); | |||
((System.ComponentModel.ISupportInitialize)(this.dgvMain)).BeginInit(); | |||
this.statusStrip1.SuspendLayout(); | |||
this.SuspendLayout(); | |||
// | |||
// dgvMain | |||
// | |||
this.dgvMain.AllowUserToAddRows = false; | |||
this.dgvMain.AllowUserToDeleteRows = false; | |||
this.dgvMain.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; | |||
this.dgvMain.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { | |||
this.SvcAddr, | |||
this.IP, | |||
this.Remarks, | |||
this.Location, | |||
this.Max, | |||
this.Min, | |||
this.Average, | |||
this.FailTime, | |||
this.Speed, | |||
this.TestSpeed}); | |||
this.dgvMain.Dock = System.Windows.Forms.DockStyle.Fill; | |||
this.dgvMain.Location = new System.Drawing.Point(0, 0); | |||
this.dgvMain.Name = "dgvMain"; | |||
this.dgvMain.ReadOnly = true; | |||
this.dgvMain.RowTemplate.Height = 23; | |||
this.dgvMain.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; | |||
this.dgvMain.Size = new System.Drawing.Size(878, 507); | |||
this.dgvMain.TabIndex = 0; | |||
this.dgvMain.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMain_CellClick); | |||
this.dgvMain.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgvMain_CellMouseDoubleClick); | |||
// | |||
// SvcAddr | |||
// | |||
this.SvcAddr.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.SvcAddr.Frozen = true; | |||
this.SvcAddr.HeaderText = "Address"; | |||
this.SvcAddr.Name = "SvcAddr"; | |||
this.SvcAddr.ReadOnly = true; | |||
this.SvcAddr.Width = 72; | |||
// | |||
// IP | |||
// | |||
this.IP.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.IP.HeaderText = "IP"; | |||
this.IP.Name = "IP"; | |||
this.IP.ReadOnly = true; | |||
this.IP.Width = 42; | |||
// | |||
// Remarks | |||
// | |||
this.Remarks.HeaderText = "Remark"; | |||
this.Remarks.Name = "Remarks"; | |||
this.Remarks.ReadOnly = true; | |||
this.Remarks.Width = 80; | |||
// | |||
// Location | |||
// | |||
this.Location.HeaderText = "Location"; | |||
this.Location.MinimumWidth = 70; | |||
this.Location.Name = "Location"; | |||
this.Location.ReadOnly = true; | |||
this.Location.Width = 150; | |||
// | |||
// Max | |||
// | |||
this.Max.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.Max.HeaderText = "Max"; | |||
this.Max.Name = "Max"; | |||
this.Max.ReadOnly = true; | |||
this.Max.Width = 48; | |||
// | |||
// Min | |||
// | |||
this.Min.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.Min.HeaderText = "Min"; | |||
this.Min.Name = "Min"; | |||
this.Min.ReadOnly = true; | |||
this.Min.Width = 48; | |||
// | |||
// Average | |||
// | |||
this.Average.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.Average.HeaderText = "Average"; | |||
this.Average.Name = "Average"; | |||
this.Average.ReadOnly = true; | |||
this.Average.Width = 72; | |||
// | |||
// FailTime | |||
// | |||
this.FailTime.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.FailTime.HeaderText = "FailTime"; | |||
this.FailTime.Name = "FailTime"; | |||
this.FailTime.ReadOnly = true; | |||
this.FailTime.Width = 78; | |||
// | |||
// Speed | |||
// | |||
this.Speed.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.Speed.HeaderText = "Speed"; | |||
this.Speed.Name = "Speed"; | |||
this.Speed.ReadOnly = true; | |||
this.Speed.Width = 60; | |||
// | |||
// TestSpeed | |||
// | |||
this.TestSpeed.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; | |||
this.TestSpeed.HeaderText = "TestSpeed"; | |||
this.TestSpeed.Name = "TestSpeed"; | |||
this.TestSpeed.ReadOnly = true; | |||
this.TestSpeed.Width = 65; | |||
// | |||
// statusStrip1 | |||
// | |||
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { | |||
this.tssStatusLabel, | |||
this.tssStatus, | |||
this.toolStripStatusLabel1, | |||
this.pBar}); | |||
this.statusStrip1.Location = new System.Drawing.Point(0, 485); | |||
this.statusStrip1.Name = "statusStrip1"; | |||
this.statusStrip1.Size = new System.Drawing.Size(878, 22); | |||
this.statusStrip1.TabIndex = 1; | |||
this.statusStrip1.Text = "statusStrip1"; | |||
// | |||
// tssStatusLabel | |||
// | |||
this.tssStatusLabel.Name = "tssStatusLabel"; | |||
this.tssStatusLabel.Size = new System.Drawing.Size(89, 17); | |||
this.tssStatusLabel.Text = "CurrentStatus:"; | |||
// | |||
// tssStatus | |||
// | |||
this.tssStatus.Name = "tssStatus"; | |||
this.tssStatus.Size = new System.Drawing.Size(55, 17); | |||
this.tssStatus.Text = "Unknow"; | |||
// | |||
// toolStripStatusLabel1 | |||
// | |||
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; | |||
this.toolStripStatusLabel1.Size = new System.Drawing.Size(719, 17); | |||
this.toolStripStatusLabel1.Spring = true; | |||
// | |||
// pBar | |||
// | |||
this.pBar.Name = "pBar"; | |||
this.pBar.Size = new System.Drawing.Size(100, 16); | |||
this.pBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee; | |||
this.pBar.Visible = false; | |||
// | |||
// PingForm | |||
// | |||
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); | |||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; | |||
this.AutoSize = true; | |||
this.ClientSize = new System.Drawing.Size(878, 507); | |||
this.Controls.Add(this.statusStrip1); | |||
this.Controls.Add(this.dgvMain); | |||
this.Name = "PingForm"; | |||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; | |||
this.Text = "PingForm"; | |||
((System.ComponentModel.ISupportInitialize)(this.dgvMain)).EndInit(); | |||
this.statusStrip1.ResumeLayout(false); | |||
this.statusStrip1.PerformLayout(); | |||
this.ResumeLayout(false); | |||
this.PerformLayout(); | |||
} | |||
#endregion | |||
private System.Windows.Forms.DataGridView dgvMain; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Local; | |||
private System.Windows.Forms.StatusStrip statusStrip1; | |||
private System.Windows.Forms.ToolStripStatusLabel tssStatusLabel; | |||
private System.Windows.Forms.ToolStripStatusLabel tssStatus; | |||
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; | |||
private System.Windows.Forms.ToolStripProgressBar pBar; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn SvcAddr; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn IP; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Remarks; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Location; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Max; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Min; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Average; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn FailTime; | |||
private System.Windows.Forms.DataGridViewTextBoxColumn Speed; | |||
private System.Windows.Forms.DataGridViewLinkColumn TestSpeed; | |||
} | |||
} |
@@ -1,325 +0,0 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.ComponentModel; | |||
using System.Data; | |||
using System.Drawing; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Net.NetworkInformation; | |||
using System.Text; | |||
using System.Text.RegularExpressions; | |||
using System.Threading; | |||
using System.Windows.Forms; | |||
using Shadowsocks.Controller; | |||
using Shadowsocks.Model; | |||
using Shadowsocks.Properties; | |||
using Shadowsocks._3rd; | |||
using Shadowsocks._3rd.ProxySocket; | |||
namespace Shadowsocks.View | |||
{ | |||
public sealed partial class PingForm : Form | |||
{ | |||
private readonly ShadowsocksController controller; | |||
#region Delegate | |||
private delegate void selectMinMax(); | |||
private void SelectMinMax() | |||
{ | |||
if (dgvMain.InvokeRequired) | |||
{ | |||
var invoke = new selectMinMax(SelectMinMax); | |||
Invoke(invoke); | |||
} | |||
else | |||
{ | |||
var q = dgvMain.Rows.Cast<DataGridViewRow>().Where(row => (int)row.Cells["Average"].Value != 9999).ToArray(); | |||
int max = q.Max(x => Convert.ToInt32(x.Cells["Average"].Value)); | |||
int min = q.Min(x => Convert.ToInt32(x.Cells["Average"].Value)); | |||
foreach (DataGridViewRow row in dgvMain.Rows) | |||
{ | |||
if ((int)row.Cells["Average"].Value == min) row.DefaultCellStyle.ForeColor = Color.Green; | |||
if ((int)row.Cells["Average"].Value == max) row.DefaultCellStyle.ForeColor = Color.Red; | |||
} | |||
} | |||
} | |||
private delegate void changeStatus(string val); | |||
private void ChangeStatus(string val) | |||
{ | |||
if (statusStrip1.InvokeRequired) | |||
{ | |||
var invoke = new changeStatus(ChangeStatus); | |||
Invoke(invoke, val); | |||
} | |||
else | |||
{ | |||
if (val == I18N.GetString("Ready")) | |||
{ | |||
pBar.Visible = false; | |||
dgvMain.Enabled = true; | |||
tssStatus.Text = I18N.GetString("Ready"); | |||
tssStatus.ForeColor = Color.Green; | |||
} | |||
else if (val == I18N.GetString("Wrong")) | |||
{ | |||
pBar.Visible = false; | |||
dgvMain.Enabled = true; | |||
tssStatus.Text = I18N.GetString("Wrong"); | |||
tssStatus.ForeColor = Color.Red; | |||
} | |||
else | |||
{ | |||
pBar.Visible = true; | |||
dgvMain.Enabled = false; | |||
tssStatus.Text = val; | |||
tssStatus.ForeColor = Color.Blue; | |||
} | |||
} | |||
} | |||
private delegate void modifyRow(int rowID, string ip,string loc, int max, int min, int avg, int failtime); | |||
private void ModifyRow(int rowID, string ip,string loc, int max, int min, int avg, int failtime) | |||
{ | |||
if (dgvMain.InvokeRequired) | |||
{ | |||
var del = new modifyRow(ModifyRow); | |||
Invoke(del, new object[] { rowID, ip,loc, max, min, avg, failtime }); | |||
} | |||
else | |||
{ | |||
dgvMain.Rows[rowID].Cells[1].Value = string.IsNullOrEmpty(ip) ? I18N.GetString("PingFail") : ip; | |||
dgvMain.Rows[rowID].Cells[4].Value = max; | |||
dgvMain.Rows[rowID].Cells[5].Value = min; | |||
dgvMain.Rows[rowID].Cells[6].Value = avg; | |||
dgvMain.Rows[rowID].Cells[7].Value = failtime; | |||
if (!string.IsNullOrEmpty(loc)) dgvMain.Rows[rowID].Cells[3].Value = loc; | |||
} | |||
} | |||
private delegate void modifyRowSpeed(int rowID,string ip, string loc,string speed); | |||
private void ModifyRowSpeed(int rowID, string ip, string loc, string speed) | |||
{ | |||
if (dgvMain.InvokeRequired) | |||
{ | |||
var del = new modifyRowSpeed(ModifyRowSpeed); | |||
Invoke(del, new object[] {rowID, ip, loc, speed}); | |||
} | |||
else | |||
{ | |||
dgvMain.Rows[rowID].Cells[1].Value = string.IsNullOrEmpty(ip) ? I18N.GetString("PingFail") : ip; | |||
if (!string.IsNullOrEmpty(loc)) dgvMain.Rows[rowID].Cells[3].Value = loc; | |||
if (!string.IsNullOrEmpty(speed)) dgvMain.Rows[rowID].Cells[8].Value = speed; | |||
} | |||
} | |||
#endregion | |||
#region Method | |||
private void Ping(object r) | |||
{ | |||
var row = r as DataGridViewRow; | |||
if (row == null) return; | |||
var addr = ""; | |||
var result = new List<long>(); | |||
var failTime = 0; | |||
using (var ping = new Ping()) | |||
{ | |||
for (var times = 0; times < 4; times++) | |||
{ | |||
try | |||
{ | |||
var reply = ping.Send((string)row.Cells[0].Value, 2000); | |||
if (reply == null) { failTime++; continue;} | |||
addr = reply.Address.ToString(); | |||
if (reply.Status == IPStatus.Success) | |||
{ | |||
result.Add(reply.RoundtripTime); | |||
} | |||
else | |||
{ | |||
failTime++; | |||
} | |||
} | |||
catch | |||
{ | |||
failTime++; | |||
} | |||
} | |||
} | |||
var location = ""; | |||
if (Util.Utils.qqwry != null & addr != "") | |||
{ | |||
var v = Util.Utils.qqwry.SearchIPLocation(addr); | |||
location = (v.country + v.area.Replace("CZ88.NET", "")); | |||
} | |||
if(result.Count == 0) | |||
ModifyRow(row.Index, addr,location, 9999, 9999, 9999, 10); | |||
else | |||
ModifyRow(row.Index, addr,location, (int)result.Max(), (int)result.Min(), (int)result.Average(), failTime); | |||
} | |||
private void Go(object rc) | |||
{ | |||
var rows = rc as DataGridViewRowCollection; | |||
if (rows != null) | |||
{ | |||
foreach (DataGridViewRow row in rows) | |||
Ping(row); | |||
SelectMinMax(); | |||
ChangeStatus(I18N.GetString("Ready")); | |||
} | |||
else | |||
{ | |||
ChangeStatus(I18N.GetString("Wrong")); | |||
} | |||
} | |||
private void Test(object index) | |||
{ | |||
try | |||
{ | |||
var rowIndex = (int)index; | |||
string ip, location; | |||
float speed; | |||
GetIPAddress(rowIndex, out ip, out location, out speed); | |||
ModifyRowSpeed(rowIndex, ip, location, speed.ToString("N0") + "KB/s"); | |||
ChangeStatus(I18N.GetString("Ready")); | |||
} | |||
catch | |||
{ | |||
ChangeStatus(I18N.GetString("Wrong")); | |||
} | |||
} | |||
private void GetIPAddress(int svc, out string addr, out string stat, out float speed) | |||
{ | |||
var currentIndex = controller.GetConfiguration().index; | |||
controller.SelectServerIndexTemp(svc); | |||
var webClient = new SocksWebClient { ProxyDetails = new ProxyDetails(controller.GetConfiguration().localPort)}; | |||
try | |||
{ | |||
//get location | |||
var regx1 = new Regex(@"\d+\.\d+\.\d+\.\d+"); | |||
var regx2 = new Regex(@"来自:(.*?)\<"); | |||
var response = webClient.DownloadString(@"http://1111.ip138.com/ic.asp"); | |||
var mc1 = regx1.Match(response); | |||
addr = mc1.Success ? mc1.Value : I18N.GetString("Unknow"); | |||
var mc2 = regx2.Match(response); | |||
stat = mc2.Success ? mc2.Groups[1].Value : I18N.GetString("…(⊙_⊙;)…"); | |||
} | |||
catch | |||
{ | |||
addr = I18N.GetString("Unknow"); | |||
stat = I18N.GetString("…(⊙_⊙;)…"); | |||
} | |||
try | |||
{ | |||
//speed test | |||
var sw = System.Diagnostics.Stopwatch.StartNew(); | |||
var dl = webClient.DownloadData("https://dl.google.com/tag/s/appguid%3D%7B8A69D345-D564-463C-AFF1-A69D9E530F96%7D%26iid%3D%7B7B1E2CBF-95F1-5FDD-C836-E5930E3E51CD%7D%26lang%3Den%26browser%3D4%26usagestats%3D0%26appname%3DGoogle%2520Chrome%26needsadmin%3Dprefers%26installdataindex%3Ddefaultbrowser/update2/installers/ChromeSetup.exe");//http://dl.google.com/googletalk/googletalk-setup.exe | |||
sw.Stop(); | |||
var len = dl.Length / 1024f; | |||
var sec = sw.Elapsed.Milliseconds / 1000f; | |||
speed = len / sec; | |||
} | |||
catch | |||
{ | |||
speed = 0; | |||
} | |||
webClient.Dispose(); | |||
controller.SelectServerIndexTemp(currentIndex); | |||
} | |||
#endregion | |||
public PingForm(ShadowsocksController sc) | |||
{ | |||
InitializeComponent(); | |||
var qqwryPath = Environment.CurrentDirectory + "\\qqwry.dat"; | |||
if (Util.Utils.qqwry == null && File.Exists(qqwryPath)) Util.Utils.qqwry = new QQWry(qqwryPath); | |||
controller = sc; | |||
Font = Util.Utils.GetFont(); | |||
PerformLayout(); | |||
UpdateTexts(); | |||
Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); | |||
LoadConfiguration(controller.GetConfiguration()); | |||
if (dgvMain.Rows.Count <= 5) | |||
{ | |||
foreach (var row in dgvMain.Rows) | |||
{ | |||
var t = new Thread(Ping) { IsBackground = true }; | |||
t.Start(row); | |||
} | |||
} | |||
else | |||
{ | |||
ChangeStatus(I18N.GetString("DoSomething")); | |||
var t = new Thread(Go) { IsBackground = true }; | |||
t.Start(dgvMain.Rows); | |||
} | |||
} | |||
private void UpdateTexts() | |||
{ | |||
foreach (DataGridViewColumn col in dgvMain.Columns) | |||
col.HeaderText = I18N.GetString(col.Name); | |||
Text = I18N.GetString("UsableTest"); | |||
tssStatusLabel.Text = I18N.GetString("CurrentStatus:"); | |||
tssStatus.Text = I18N.GetString("Ready"); | |||
} | |||
private void LoadConfiguration(Configuration configuration) | |||
{ | |||
dgvMain.Rows.Clear(); | |||
foreach (Server server in configuration.configs) | |||
{ | |||
int index = dgvMain.Rows.Add(); | |||
dgvMain.Rows[index].Cells[0].Value = server.server; | |||
dgvMain.Rows[index].Cells[1].Value = I18N.GetString("Pinging"); | |||
dgvMain.Rows[index].Cells[2].Value = server.remarks; | |||
dgvMain.Rows[index].Cells[9].Value = I18N.GetString("TestSpeed"); | |||
} | |||
var row = dgvMain.Rows[configuration.index]; | |||
dgvMain.ClearSelection(); | |||
dgvMain.CurrentCell = row.Cells[0]; | |||
row.Selected = true; | |||
} | |||
private void dgvMain_CellClick(object sender, DataGridViewCellEventArgs e) | |||
{ | |||
if (dgvMain.Columns[e.ColumnIndex].Name != "TestSpeed") return; | |||
ChangeStatus(I18N.GetString("DoSomething")); | |||
var t = new Thread(Test) {IsBackground = true}; | |||
t.Start(e.RowIndex); | |||
//10sec time out because WebClient dont have Timeout property | |||
var tm = new System.Timers.Timer(10000); | |||
tm.Elapsed += (ss, ee) =>{ tm.Stop(); t.Abort(); }; | |||
tm.Start(); | |||
} | |||
private void dgvMain_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) | |||
{ | |||
if (e.RowIndex < 0) return; | |||
if ((int) dgvMain.Rows[e.RowIndex].Cells[4].Value == 9999) | |||
{ if (MessageBox.Show(I18N.GetString("Seems your server is down, r u sure apply this server?"), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; } | |||
controller.SelectServerIndex(e.RowIndex); | |||
Close(); | |||
} | |||
} | |||
} |
@@ -29,6 +29,7 @@ | |||
private void InitializeComponent() | |||
{ | |||
this.pictureBox1 = new System.Windows.Forms.PictureBox(); | |||
this.listBox1 = new System.Windows.Forms.ListBox(); | |||
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); | |||
this.SuspendLayout(); | |||
// | |||
@@ -42,6 +43,19 @@ | |||
this.pictureBox1.TabIndex = 1; | |||
this.pictureBox1.TabStop = false; | |||
// | |||
// listBox1 | |||
// | |||
this.listBox1.DisplayMember = "Value"; | |||
this.listBox1.FormattingEnabled = true; | |||
this.listBox1.ItemHeight = 12; | |||
this.listBox1.Location = new System.Drawing.Point(224, 10); | |||
this.listBox1.Name = "listBox1"; | |||
this.listBox1.ScrollAlwaysVisible = true; | |||
this.listBox1.Size = new System.Drawing.Size(227, 208); | |||
this.listBox1.TabIndex = 2; | |||
this.listBox1.ValueMember = "Key"; | |||
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); | |||
// | |||
// QRCodeForm | |||
// | |||
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); | |||
@@ -49,7 +63,8 @@ | |||
this.AutoSize = true; | |||
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; | |||
this.BackColor = System.Drawing.Color.White; | |||
this.ClientSize = new System.Drawing.Size(338, 274); | |||
this.ClientSize = new System.Drawing.Size(457, 228); | |||
this.Controls.Add(this.listBox1); | |||
this.Controls.Add(this.pictureBox1); | |||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; | |||
this.MaximizeBox = false; | |||
@@ -67,5 +82,6 @@ | |||
#endregion | |||
private System.Windows.Forms.PictureBox pictureBox1; | |||
private System.Windows.Forms.ListBox listBox1; | |||
} | |||
} |
@@ -8,8 +8,10 @@ using System.Data; | |||
using System.Drawing; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
using Shadowsocks.Model; | |||
namespace Shadowsocks.View | |||
{ | |||
@@ -30,8 +32,8 @@ namespace Shadowsocks.View | |||
string qrText = ssconfig; | |||
QRCode code = ZXing.QrCode.Internal.Encoder.encode(qrText, ErrorCorrectionLevel.M); | |||
ByteMatrix m = code.Matrix; | |||
int blockSize = Math.Max(pictureBox1.Height / m.Height, 1); | |||
Bitmap drawArea = new Bitmap((m.Width * blockSize), (m.Height * blockSize)); | |||
int blockSize = Math.Max(pictureBox1.Height/m.Height, 1); | |||
Bitmap drawArea = new Bitmap((m.Width*blockSize), (m.Height*blockSize)); | |||
using (Graphics g = Graphics.FromImage(drawArea)) | |||
{ | |||
g.Clear(Color.White); | |||
@@ -43,7 +45,7 @@ namespace Shadowsocks.View | |||
{ | |||
if (m[row, col] != 0) | |||
{ | |||
g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize); | |||
g.FillRectangle(b, blockSize*row, blockSize*col, blockSize, blockSize); | |||
} | |||
} | |||
} | |||
@@ -54,7 +56,20 @@ namespace Shadowsocks.View | |||
private void QRCodeForm_Load(object sender, EventArgs e) | |||
{ | |||
GenQR(code); | |||
var servers = Configuration.Load(); | |||
var serverDatas = servers.configs.Select( | |||
server => | |||
new KeyValuePair<string, string>(ShadowsocksController.GetQRCode(server), server.FriendlyName()) | |||
).ToList(); | |||
listBox1.DataSource = serverDatas; | |||
var selectIndex = serverDatas.FindIndex(serverData => serverData.Key.StartsWith(code)); | |||
if (selectIndex >= 0) listBox1.SetSelected(selectIndex, true); | |||
} | |||
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) | |||
{ | |||
GenQR((sender as ListBox)?.SelectedValue.ToString()); | |||
} | |||
} | |||
} |
@@ -112,9 +112,9 @@ | |||
<value>2.0</value> | |||
</resheader> | |||
<resheader name="reader"> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
<resheader name="writer"> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | |||
</resheader> | |||
</root> |
@@ -242,7 +242,6 @@ namespace Shadowsocks.View | |||
SetBitmap(bitmap, 255); | |||
} | |||
/// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para> | |||
public void SetBitmap(Bitmap bitmap, byte opacity) | |||
{ | |||
@@ -288,7 +287,6 @@ namespace Shadowsocks.View | |||
} | |||
} | |||
protected override CreateParams CreateParams | |||
{ | |||
get | |||
@@ -1,19 +1,6 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<?xml version="1.0"?> | |||
<configuration> | |||
<startup> | |||
<supportedRuntime version="v4.0" /> | |||
<supportedRuntime version="v2.0.50727" /> | |||
</startup> | |||
<runtime> | |||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | |||
<dependentAssembly> | |||
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> | |||
<bindingRedirect oldVersion="0.0.0.0-2.6.8.0" newVersion="2.6.8.0" /> | |||
</dependentAssembly> | |||
<dependentAssembly> | |||
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> | |||
<bindingRedirect oldVersion="0.0.0.0-2.6.8.0" newVersion="2.6.8.0" /> | |||
</dependentAssembly> | |||
</assemblyBinding> | |||
</runtime> | |||
</configuration> | |||
<supportedRuntime version="v4.0"/> | |||
<supportedRuntime version="v2.0.50727"/> | |||
</startup></configuration> |
@@ -1,70 +1,75 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | |||
<PropertyGroup> | |||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |||
<ProjectGuid>{F58374A5-9AFB-430A-AF20-C509D3DCED3F}</ProjectGuid> | |||
<ProductVersion>9.0.21022</ProductVersion> | |||
<SchemaVersion>2.0</SchemaVersion> | |||
<ProjectGuid>{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}</ProjectGuid> | |||
<OutputType>WinExe</OutputType> | |||
<AppDesignerFolder>Properties</AppDesignerFolder> | |||
<RootNamespace>Shadowsocks</RootNamespace> | |||
<AssemblyName>Shadowsocks</AssemblyName> | |||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | |||
<FileAlignment>512</FileAlignment> | |||
<StartupObject> | |||
</StartupObject> | |||
<ApplicationIcon>shadowsocks.ico</ApplicationIcon> | |||
<IsWebBootstrapper>false</IsWebBootstrapper> | |||
<FileUpgradeFlags> | |||
</FileUpgradeFlags> | |||
<UpgradeBackupLocation> | |||
</UpgradeBackupLocation> | |||
<OldToolsVersion>3.5</OldToolsVersion> | |||
<TargetFrameworkProfile>Client</TargetFrameworkProfile> | |||
<PublishUrl>publish\</PublishUrl> | |||
<Install>true</Install> | |||
<InstallFrom>Disk</InstallFrom> | |||
<UpdateEnabled>false</UpdateEnabled> | |||
<UpdateMode>Foreground</UpdateMode> | |||
<UpdateInterval>7</UpdateInterval> | |||
<UpdateIntervalUnits>Days</UpdateIntervalUnits> | |||
<UpdatePeriodically>false</UpdatePeriodically> | |||
<UpdateRequired>false</UpdateRequired> | |||
<MapFileExtensions>true</MapFileExtensions> | |||
<ApplicationRevision>1</ApplicationRevision> | |||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> | |||
<UseApplicationTrust>false</UseApplicationTrust> | |||
<BootstrapperEnabled>true</BootstrapperEnabled> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |||
<PlatformTarget>x86</PlatformTarget> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> | |||
<DebugSymbols>true</DebugSymbols> | |||
<OutputPath>bin\x86\Debug\</OutputPath> | |||
<DefineConstants>TRACE;DEBUG</DefineConstants> | |||
<DebugType>full</DebugType> | |||
<Optimize>false</Optimize> | |||
<OutputPath>bin\Debug\</OutputPath> | |||
<DefineConstants>DEBUG;TRACE</DefineConstants> | |||
<PlatformTarget>x86</PlatformTarget> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet> | |||
<Prefer32Bit>false</Prefer32Bit> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |||
<PlatformTarget>x86</PlatformTarget> | |||
<DebugType>pdbonly</DebugType> | |||
<Optimize>true</Optimize> | |||
<OutputPath>bin\Release\</OutputPath> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> | |||
<OutputPath>bin\x86\Release\</OutputPath> | |||
<DefineConstants>TRACE</DefineConstants> | |||
<Optimize>true</Optimize> | |||
<DebugType>pdbonly</DebugType> | |||
<PlatformTarget>x86</PlatformTarget> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet> | |||
<Prefer32Bit>false</Prefer32Bit> | |||
<DebugSymbols>true</DebugSymbols> | |||
</PropertyGroup> | |||
<PropertyGroup> | |||
<ApplicationIcon>shadowsocks.ico</ApplicationIcon> | |||
<ApplicationManifest>app.manifest</ApplicationManifest> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="Microsoft.VisualBasic" /> | |||
<Reference Include="System" /> | |||
<Reference Include="System.Core" /> | |||
<Reference Include="System.Net" /> | |||
<Reference Include="System.Xml.Linq" /> | |||
<Reference Include="System.Data.DataSetExtensions" /> | |||
<Reference Include="Microsoft.CSharp" /> | |||
<Reference Include="System.Data" /> | |||
<Reference Include="System.Deployment" /> | |||
<Reference Include="System.Drawing" /> | |||
<Reference Include="System.Windows.Forms" /> | |||
<Reference Include="System.Xml" /> | |||
<Reference Include="System.XML" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="3rd\ProxySocket\AuthMethod.cs" /> | |||
<Compile Include="3rd\ProxySocket\AuthNone.cs" /> | |||
<Compile Include="3rd\ProxySocket\AuthUserPass.cs" /> | |||
<Compile Include="3rd\ProxySocket\IAsyncProxyResult.cs" /> | |||
<Compile Include="3rd\ProxySocket\ProxyException.cs" /> | |||
<Compile Include="3rd\ProxySocket\ProxySocket.cs" /> | |||
<Compile Include="3rd\ProxySocket\Socks4Handler.cs" /> | |||
<Compile Include="3rd\ProxySocket\Socks5Handler.cs" /> | |||
<Compile Include="3rd\ProxySocket\SocksHandler.cs" /> | |||
<Compile Include="3rd\ProxySocket\SocksHttpWebRequest.cs" /> | |||
<Compile Include="3rd\ProxySocket\SocksHttpWebResponse.cs" /> | |||
<Compile Include="3rd\ProxySocket\SocksWebClient.cs"> | |||
<SubType>Component</SubType> | |||
</Compile> | |||
<Compile Include="3rd\QQWry.cs" /> | |||
<Compile Include="3rd\SimpleJson.cs" /> | |||
<Compile Include="3rd\zxing\BarcodeFormat.cs" /> | |||
<Compile Include="3rd\zxing\BaseLuminanceSource.cs" /> | |||
<Compile Include="3rd\zxing\Binarizer.cs" /> | |||
@@ -111,38 +116,45 @@ | |||
<Compile Include="3rd\zxing\qrcode\encoder\MaskUtil.cs" /> | |||
<Compile Include="3rd\zxing\qrcode\encoder\MatrixUtil.cs" /> | |||
<Compile Include="3rd\zxing\qrcode\encoder\QRCode.cs" /> | |||
<Compile Include="3rd\SimpleJson.cs" /> | |||
<Compile Include="3rd\zxing\qrcode\QRCodeReader.cs" /> | |||
<Compile Include="3rd\zxing\Result.cs" /> | |||
<Compile Include="3rd\zxing\ResultMetadataType.cs" /> | |||
<Compile Include="3rd\zxing\ResultPoint.cs" /> | |||
<Compile Include="3rd\zxing\ResultPointCallback.cs" /> | |||
<Compile Include="3rd\zxing\WriterException.cs" /> | |||
<Compile Include="Controller\AutoStartup.cs" /> | |||
<Compile Include="Controller\Service\AvailabilityStatistics.cs" /> | |||
<Compile Include="Controller\Strategy\HighAvailabilityStrategy.cs" /> | |||
<Compile Include="Controller\Strategy\SimplyChooseByStatisticsStrategy.cs" /> | |||
<Compile Include="Controller\System\AutoStartup.cs" /> | |||
<Compile Include="Controller\FileManager.cs" /> | |||
<Compile Include="Controller\GfwListUpdater.cs" /> | |||
<Compile Include="Controller\Service\GFWListUpdater.cs" /> | |||
<Compile Include="Controller\I18N.cs" /> | |||
<Compile Include="Controller\Listener.cs" /> | |||
<Compile Include="Controller\Local.cs" /> | |||
<Compile Include="Controller\Service\Listener.cs" /> | |||
<Compile Include="Controller\Logging.cs" /> | |||
<Compile Include="Controller\PACServer.cs" /> | |||
<Compile Include="Controller\PolipoRunner.cs" /> | |||
<Compile Include="Controller\PortForwarder.cs" /> | |||
<Compile Include="Controller\ShadowsocksController.cs" /> | |||
<Compile Include="Controller\SystemProxy.cs" /> | |||
<Compile Include="Controller\UpdateChecker.cs" /> | |||
<Compile Include="Controller\Service\PortForwarder.cs" /> | |||
<Compile Include="Controller\Service\UDPRelay.cs" /> | |||
<Compile Include="Controller\Service\UpdateChecker.cs" /> | |||
<Compile Include="Encryption\EncryptorBase.cs" /> | |||
<Compile Include="Encryption\EncryptorFactory.cs" /> | |||
<Compile Include="Encryption\IEncryptor.cs" /> | |||
<Compile Include="Encryption\IVEncryptor.cs" /> | |||
<Compile Include="Encryption\PolarSSL.cs" /> | |||
<Compile Include="Encryption\PolarSSLEncryptor.cs" /> | |||
<Compile Include="Encryption\Sodium.cs" /> | |||
<Compile Include="Encryption\SodiumEncryptor.cs" /> | |||
<Compile Include="Encryption\TableEncryptor.cs" /> | |||
<Compile Include="Model\Configuration.cs" /> | |||
<Compile Include="Encryption\IEncryptor.cs" /> | |||
<Compile Include="Controller\Service\PACServer.cs" /> | |||
<Compile Include="Model\Server.cs" /> | |||
<Compile Include="Program.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
<Compile Include="Model\Configuration.cs" /> | |||
<Compile Include="Properties\Resources.Designer.cs"> | |||
<AutoGen>True</AutoGen> | |||
<DesignTime>True</DesignTime> | |||
<DependentUpon>Resources.resx</DependentUpon> | |||
</Compile> | |||
<Compile Include="Controller\Strategy\BalancingStrategy.cs" /> | |||
<Compile Include="Controller\Strategy\StrategyManager.cs" /> | |||
<Compile Include="Controller\Strategy\IStrategy.cs" /> | |||
<Compile Include="Util\Util.cs" /> | |||
<Compile Include="View\ConfigForm.cs"> | |||
<SubType>Form</SubType> | |||
@@ -150,13 +162,19 @@ | |||
<Compile Include="View\ConfigForm.Designer.cs"> | |||
<DependentUpon>ConfigForm.cs</DependentUpon> | |||
</Compile> | |||
<Compile Include="View\MenuViewController.cs" /> | |||
<Compile Include="View\PingForm.cs"> | |||
<Compile Include="Controller\Service\TCPRelay.cs" /> | |||
<Compile Include="Controller\Service\PolipoRunner.cs" /> | |||
<Compile Include="Program.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
<Compile Include="Controller\ShadowsocksController.cs" /> | |||
<Compile Include="Controller\System\SystemProxy.cs" /> | |||
<Compile Include="View\LogForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||
<Compile Include="View\PingForm.Designer.cs"> | |||
<DependentUpon>PingForm.cs</DependentUpon> | |||
<Compile Include="View\LogForm.Designer.cs"> | |||
<DependentUpon>LogForm.cs</DependentUpon> | |||
</Compile> | |||
<Compile Include="View\MenuViewController.cs" /> | |||
<Compile Include="View\QRCodeForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||
@@ -166,42 +184,73 @@ | |||
<Compile Include="View\QRCodeSplashForm.cs"> | |||
<SubType>Form</SubType> | |||
</Compile> | |||
<EmbeddedResource Include="View\ConfigForm.resx"> | |||
<DependentUpon>ConfigForm.cs</DependentUpon> | |||
<SubType>Designer</SubType> | |||
</EmbeddedResource> | |||
<EmbeddedResource Include="Properties\Resources.resx"> | |||
<Generator>ResXFileCodeGenerator</Generator> | |||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | |||
<SubType>Designer</SubType> | |||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | |||
</EmbeddedResource> | |||
<Compile Include="Properties\Resources.Designer.cs"> | |||
<AutoGen>True</AutoGen> | |||
<DependentUpon>Resources.resx</DependentUpon> | |||
<DesignTime>True</DesignTime> | |||
</Compile> | |||
<EmbeddedResource Include="View\ConfigForm.resx"> | |||
<DependentUpon>ConfigForm.cs</DependentUpon> | |||
</EmbeddedResource> | |||
<EmbeddedResource Include="View\PingForm.resx"> | |||
<DependentUpon>PingForm.cs</DependentUpon> | |||
<EmbeddedResource Include="View\LogForm.resx"> | |||
<DependentUpon>LogForm.cs</DependentUpon> | |||
</EmbeddedResource> | |||
<EmbeddedResource Include="View\QRCodeForm.resx"> | |||
<DependentUpon>QRCodeForm.cs</DependentUpon> | |||
</EmbeddedResource> | |||
<None Include="app.config" /> | |||
<None Include="app.manifest" /> | |||
<None Include="app.manifest"> | |||
<SubType>Designer</SubType> | |||
</None> | |||
<None Include="Data\abp.js.gz" /> | |||
<None Include="Data\libsscrypto.dll.gz" /> | |||
<None Include="Data\polipo.exe.gz" /> | |||
<None Include="Data\mgwz.dll.gz" /> | |||
<None Include="Data\privoxy.exe.gz" /> | |||
<None Include="Data\proxy.pac.txt.gz" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="Resources\ss20.png" /> | |||
<None Include="Resources\ss16.png" /> | |||
<None Include="Resources\ss24.png" /> | |||
<None Include="Resources\ssw128.png" /> | |||
<Content Include="Data\cn.txt" /> | |||
<Content Include="Data\polipo_config.txt" /> | |||
<Content Include="Data\privoxy_conf.txt" /> | |||
<Content Include="Data\user-rule.txt" /> | |||
<Content Include="Resources\ss16.png" /> | |||
<Content Include="Resources\ss20.png" /> | |||
<Content Include="Resources\ss24.png" /> | |||
<Content Include="Resources\ssw128.png" /> | |||
<Content Include="shadowsocks.ico" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5"> | |||
<Visible>False</Visible> | |||
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName> | |||
<Install>false</Install> | |||
</BootstrapperPackage> | |||
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0"> | |||
<Visible>False</Visible> | |||
<ProductName>.NET Framework 2.0 %28x86%29</ProductName> | |||
<Install>false</Install> | |||
</BootstrapperPackage> | |||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.0"> | |||
<Visible>False</Visible> | |||
<ProductName>.NET Framework 3.0 %28x86%29</ProductName> | |||
<Install>false</Install> | |||
</BootstrapperPackage> | |||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5"> | |||
<Visible>False</Visible> | |||
<ProductName>.NET Framework 3.5</ProductName> | |||
<Install>true</Install> | |||
</BootstrapperPackage> | |||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> | |||
<Visible>False</Visible> | |||
<ProductName>.NET Framework 3.5 SP1</ProductName> | |||
<Install>false</Install> | |||
</BootstrapperPackage> | |||
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1"> | |||
<Visible>False</Visible> | |||
<ProductName>Windows Installer 3.1</ProductName> | |||
<Install>true</Install> | |||
</BootstrapperPackage> | |||
</ItemGroup> | |||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | |||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | |||
Other similar extension points exist, see Microsoft.Common.targets. | |||