Repository: CGDF-Github/SSRR-Windows Branch: master Commit: fe8acb4de7be Files: 171 Total size: 1.7 MB Directory structure: gitextract_vj9icqh0/ ├── .github/ │ └── issue_template.md ├── .gitignore ├── CHANGES ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── appveyor.yml ├── packaging/ │ └── upload.sh ├── shadowsocks-csharp/ │ ├── 3rd/ │ │ ├── SimpleJson.cs │ │ ├── opendns/ │ │ │ ├── DnsQuery.cs │ │ │ ├── DnsResponse.cs │ │ │ ├── Enum.cs │ │ │ └── RR/ │ │ │ ├── Address.cs │ │ │ ├── MX.cs │ │ │ ├── ResourceRecord.cs │ │ │ ├── ResourceRecordCollection.cs │ │ │ └── SOA.cs │ │ └── zxing/ │ │ ├── BarcodeFormat.cs │ │ ├── BaseLuminanceSource.cs │ │ ├── Binarizer.cs │ │ ├── BinaryBitmap.cs │ │ ├── BitmapLuminanceSource.cs │ │ ├── DecodeHintType.cs │ │ ├── EncodeHintType.cs │ │ ├── LuminanceSource.cs │ │ ├── Result.cs │ │ ├── ResultMetadataType.cs │ │ ├── ResultPoint.cs │ │ ├── ResultPointCallback.cs │ │ ├── WriterException.cs │ │ ├── common/ │ │ │ ├── BitArray.cs │ │ │ ├── BitMatrix.cs │ │ │ ├── BitSource.cs │ │ │ ├── DecoderResult.cs │ │ │ ├── DefaultGridSampler.cs │ │ │ ├── DetectorResult.cs │ │ │ ├── GlobalHistogramBinarizer.cs │ │ │ ├── GridSampler.cs │ │ │ ├── HybridBinarizer.cs │ │ │ ├── PerspectiveTransform.cs │ │ │ ├── StringUtils.cs │ │ │ ├── detector/ │ │ │ │ └── MathUtils.cs │ │ │ └── reedsolomon/ │ │ │ ├── GenericGF.cs │ │ │ ├── GenericGFPoly.cs │ │ │ ├── ReedSolomonDecoder.cs │ │ │ └── ReedSolomonEncoder.cs │ │ └── qrcode/ │ │ ├── QRCodeReader.cs │ │ ├── decoder/ │ │ │ ├── BitMatrixParser.cs │ │ │ ├── DataBlock.cs │ │ │ ├── DataMask.cs │ │ │ ├── DecodedBitStreamParser.cs │ │ │ ├── Decoder.cs │ │ │ ├── ErrorCorrectionLevel.cs │ │ │ ├── FormatInformation.cs │ │ │ ├── Mode.cs │ │ │ ├── QRCodeDecoderMetaData.cs │ │ │ └── Version.cs │ │ ├── detector/ │ │ │ ├── AlignmentPattern.cs │ │ │ ├── AlignmentPatternFinder.cs │ │ │ ├── Detector.cs │ │ │ ├── FinderPattern.cs │ │ │ ├── FinderPatternFinder.cs │ │ │ └── FinderPatternInfo.cs │ │ └── encoder/ │ │ ├── BlockPair.cs │ │ ├── ByteMatrix.cs │ │ ├── Encoder.cs │ │ ├── MaskUtil.cs │ │ ├── MatrixUtil.cs │ │ └── QRCode.cs │ ├── Controller/ │ │ ├── APIServer.cs │ │ ├── AutoStartup.cs │ │ ├── FileManager.cs │ │ ├── GfwListUpdater.cs │ │ ├── HttpPortForwarder.cs │ │ ├── HttpProxy.cs │ │ ├── HttpProxyRunner.cs │ │ ├── I18N.cs │ │ ├── Listener.cs │ │ ├── Local.cs │ │ ├── Logging.cs │ │ ├── PACServer.cs │ │ ├── ProxyAuth.cs │ │ ├── ProxySocket.cs │ │ ├── ShadowsocksController.cs │ │ ├── Socks5Forwarder.cs │ │ ├── SpeedTest.cs │ │ ├── SystemProxy.cs │ │ ├── UpdateChecker.cs │ │ └── UpdateFreeNode.cs │ ├── Data/ │ │ ├── cn.txt │ │ ├── privoxy_conf.txt │ │ ├── user-rule.txt │ │ └── zh-tw.txt │ ├── Encryption/ │ │ ├── EncryptorBase.cs │ │ ├── EncryptorFactory.cs │ │ ├── IEncryptor.cs │ │ ├── IVEncryptor.cs │ │ ├── Libcrypto.cs │ │ ├── LibcryptoEncryptor.cs │ │ ├── MbedTLS.cs │ │ ├── MbedTLSEncryptor.cs │ │ ├── RSA.cs │ │ ├── Sodium.cs │ │ └── SodiumEncryptor.cs │ ├── Model/ │ │ ├── Configuration.cs │ │ ├── Host.cs │ │ ├── IPRangeSet.cs │ │ ├── IPSegment.cs │ │ ├── LRUCache.cs │ │ ├── MinSearchTree.cs │ │ ├── Server.cs │ │ ├── ServerSelectStrategy.cs │ │ └── ServerSpeedLog.cs │ ├── Obfs/ │ │ ├── Auth.cs │ │ ├── AuthAkarin.cs │ │ ├── AuthChain.cs │ │ ├── HttpSimpleObfs.cs │ │ ├── IObfs.cs │ │ ├── ObfsBase.cs │ │ ├── ObfsFactory.cs │ │ ├── Plain.cs │ │ └── VerifySimpleObfs.cs │ ├── Program.cs │ ├── Properties/ │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── Util/ │ │ ├── Base64.cs │ │ ├── CRC.cs │ │ ├── EnvCheck.cs │ │ ├── ServerName.cs │ │ └── Util.cs │ ├── View/ │ │ ├── ConfigForm.Designer.cs │ │ ├── ConfigForm.cs │ │ ├── ConfigForm.resx │ │ ├── InputPassword.Designer.cs │ │ ├── InputPassword.cs │ │ ├── InputPassword.resx │ │ ├── LogForm.Designer.cs │ │ ├── LogForm.cs │ │ ├── LogForm.resx │ │ ├── MenuViewController.cs │ │ ├── PortSettingsForm.Designer.cs │ │ ├── PortSettingsForm.cs │ │ ├── PortSettingsForm.resx │ │ ├── QRCodeSplashForm.cs │ │ ├── ResetPassword.Designer.cs │ │ ├── ResetPassword.cs │ │ ├── ResetPassword.resx │ │ ├── ServerLogForm.Designer.cs │ │ ├── ServerLogForm.cs │ │ ├── ServerLogForm.resx │ │ ├── SettingsForm.Designer.cs │ │ ├── SettingsForm.cs │ │ ├── SettingsForm.resx │ │ ├── ShowTextForm.Designer.cs │ │ ├── ShowTextForm.cs │ │ ├── ShowTextForm.resx │ │ ├── SubscribeForm.Designer.cs │ │ ├── SubscribeForm.cs │ │ └── SubscribeForm.resx │ ├── app.config │ ├── app.manifest │ ├── shadowsocks-csharp-console.csproj │ ├── shadowsocks-csharp.csproj │ ├── shadowsocks-csharp4.0.csproj │ └── ssr-win-4.0.xml ├── shadowsocks-csharp.sln └── test/ ├── Properties/ │ └── AssemblyInfo.cs ├── ServerTest.cs ├── UnitTest.cs └── test.csproj ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/issue_template.md ================================================ 本项目不再进行功能性更新,仅接受BUG反馈。 请谨慎斟酌自己的issue是否值得一提。 作者耐心很差,请勿提傻逼issue浪费作者时间。 傻逼issue会被lock,傻逼会被block。 ================================================ FILE: .gitignore ================================================ Backup/ bin/ obj/ pub/ 7zip*/ temp/ shadowsocks-csharp/shadowsocks-csharp.csproj.user TestResults *.suo *.user *.exe /templates .vs/ ================================================ FILE: CHANGES ================================================ 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 - Fix GFWList PAC compatibility issue with IE11 - Encourage users to report to GFWList when no update found - Minor UI improvements 2.2.1 2015-01-18 - Fix QR Code compatibility 2.2 2015-01-14 - Support updating PAC from GFWList - Support adding server by scanning QR Code - Output timestamp in logs - Minor fixes 2.1.6 2015-01-02 - Fix OPTIONS requests - Improve logs 2.1.5 2014-12-25 - Fix QR Code compatibility with iOS - Only left button will trigger double click on tray icon 2.1.4 2014-12-20 - Fix crash when remarks are too long 2.1.3 2014-12-20 - Add Chinese Language - Fix some UI issues on Windows 8 - Fix some UI issues on high DPI screens - Log bind error more friendly - Stability issues 2.1.2 2014-12-14 - Fix sometimes Shadowsocks doesn't respond to requests 2.1.1 2014-12-14 - Add global proxy option 2.1 2014-12-12 - Add salsa20 and chacha20 support 2.0.11 2014-11-23 - Fix a crash - Only switch the system proxy off if we have switched it on 2.0.10 2014-11-18 - Minor fixes - Optimize code 2.0.9 2014-11-13 - Fix startup path - Fix allowed port range for polipo 2.0.8 2014-11-12 - Fix data corruption - Set proxy for PPPoE - Auto Startup Option - Support high DPI screens 2.0.7 2014-11-11 - Use OpenSSL for now 2.0.6 2014-11-10 - Minor bug fixes 2.0.5 2014-11-09 - Fix QRCode size - Share over LAN option - Log to temp path instead 2.0.4 2014-11-09 - Try to fix data corruption - Remove all configuration except x86 2.0.3 2014-11-08 - Support QRCode generation - Fix compatibility issues with some Chrome version 2.0.2 2014-11-08 - Add remarks - Fix error when polipo is killed 2.0.1 2014-11-08 - Check already running 2.0 2014-11-08 - Initial release ================================================ FILE: CONTRIBUTING.md ================================================ How to Contribute ================= Pull Requests ------------- 1. Pull requests are welcome. 2. Make sure to pass the unit tests. Write unit tests for new modules if needed. Issues ------ 1. Nobody has reported any bugs but posted a lot of questions in the last few months. So we're closing the issue tracker. [Troubleshooting]: https://github.com/clowwindy/shadowsocks/wiki/Troubleshooting [mailing lists]: https://groups.google.com/forum/#!forum/shadowsocks ================================================ FILE: LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. 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 them 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 prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. 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. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey 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; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If 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 convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU 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 that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. 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. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 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. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. 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 state 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 3 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, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: {project} Copyright (C) {year} {fullname} This program 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, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU 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. But first, please read . ================================================ FILE: README.md ================================================ ShadowsocksR for Windows ======================= ### 相关说明 当初本项目的目的是为了配合[ss-panel-v3-mod_Uim](https://github.com/Anankke/ss-panel-v3-mod_Uim)解决“\[SSR/SSRR\]无法使用TLS1.2的订阅链接”问题,并成为其内置的默认SSR客户端 同时应众多机场主要求,还在本项目中加入了“反全家桶机制”,过滤掉大部分(顽固的)小白。这部分坚决不放弃全家桶的小白,不属于[ss-panel-v3-mod_Uim](https://github.com/Anankke/ss-panel-v3-mod_Uim)的默认服务对象。(但是愿意服务这部分用户的机场主依然可以自行修改资源文件以向这部分用户提供服务) `补充一点细节:本项目不提供.Net2.0版的exe也是为了趋向“过滤掉大部分(顽固的)小白”这一共同目标` 至此可以发现:本项目完全不打算吸引圈外人士使用。甚至,实际上,本项目强烈不建议圈外人士自行下载本客户端使用,以免引起不必要的蛋疼,除非真的真的真的很喜欢本客户端的设计和功能。同时,也强烈不建议各博客、wiki等文字媒体收录本项目——毕竟这并不是一个面向所有大众的项目 ### 环境 .Net 4.5+ 无\[360/2345/百度\]全家桶 \[360/2345/百度\]全家桶用户请使用[SSRR - Akkariiin](https://github.com/shadowsocksrr/shadowsocksr-csharp) ### 授权 GPLv3 Copyright © Akkariiin 2018. Forked from ShadowsocksR by BreakWa11 ================================================ FILE: appveyor.yml ================================================ version: 1.0.{build} image: Visual Studio 2015 environment: matrix: - platform: Any CPU configuration: Debug - platform: Any CPU configuration: Release matrix: fast_finish: false build: parallel: true verbosity: normal artifacts: - path: shadowsocks-csharp\bin\2.0\Release\ShadowsocksR.exe name: ShadowsocksR-dotnet2.0.exe - path: shadowsocks-csharp\bin\4.0\Release\ShadowsocksR.exe name: ShadowsocksR-dotnet4.0.exe ================================================ FILE: packaging/upload.sh ================================================ #!/bin/bash version=$1 rsync --progress -e ssh shadowsocks-csharp/bin/x86/Release/Shadowsocks-win-dotnet4.0-$1.zip frs.sourceforge.net:/home/frs/project/shadowsocksgui/dist/ rsync --progress -e ssh shadowsocks-csharp/bin/x86/Release/Shadowsocks-win-$1.zip frs.sourceforge.net:/home/frs/project/shadowsocksgui/dist/ ================================================ FILE: shadowsocks-csharp/3rd/SimpleJson.cs ================================================ //----------------------------------------------------------------------- // // Copyright (c) 2011, The Outercurve Foundation. // // Licensed under the MIT License (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.opensource.org/licenses/mit-license.php // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // 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. // // Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me) // https://github.com/facebook-csharp-sdk/simple-json //----------------------------------------------------------------------- // VERSION: // NOTE: uncomment the following line to make SimpleJson class internal. //#define SIMPLE_JSON_INTERNAL // NOTE: uncomment the following line to make JsonArray and JsonObject class internal. //#define SIMPLE_JSON_OBJARRAYINTERNAL // NOTE: uncomment the following line to enable dynamic support. //#define SIMPLE_JSON_DYNAMIC // NOTE: uncomment the following line to enable DataContract support. //#define SIMPLE_JSON_DATACONTRACT // NOTE: uncomment the following line to use Reflection.Emit (better performance) instead of method.invoke(). // don't enable ReflectionEmit for WinRT, Silverlight and WP7. //#define SIMPLE_JSON_REFLECTIONEMIT // NOTE: uncomment the following line if you are compiling under Window Metro style application/library. // usually already defined in properties //#define NETFX_CORE; // original json parsing code from http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Drawing; #if SIMPLE_JSON_DYNAMIC using System.Dynamic; #endif using System.Globalization; using System.Reflection; #if SIMPLE_JSON_REFLECTIONEMIT using System.Reflection.Emit; #endif #if SIMPLE_JSON_DATACONTRACT using System.Runtime.Serialization; #endif using System.Text; using SimpleJson.Reflection; namespace SimpleJson { #region JsonArray /// /// Represents the json array. /// [EditorBrowsable(EditorBrowsableState.Never)] [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] #if SIMPLE_JSON_OBJARRAYINTERNAL internal #else public #endif class JsonArray : List { /// /// Initializes a new instance of the class. /// public JsonArray() { } /// /// Initializes a new instance of the class. /// /// The capacity of the json array. public JsonArray(int capacity) : base(capacity) { } /// /// The json representation of the array. /// /// The json representation of the array. public override string ToString() { return SimpleJson.SerializeObject(this) ?? string.Empty; } } #endregion #region JsonObject /// /// Represents the json object. /// [EditorBrowsable(EditorBrowsableState.Never)] [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")] #if SIMPLE_JSON_OBJARRAYINTERNAL internal #else public #endif class JsonObject : #if SIMPLE_JSON_DYNAMIC DynamicObject, #endif IDictionary { /// /// The internal member dictionary. /// private readonly Dictionary _members = new Dictionary(); /// /// Gets the at the specified index. /// /// public object this[int index] { get { return GetAtIndex(_members, index); } } internal static object GetAtIndex(IDictionary obj, int index) { if (obj == null) throw new ArgumentNullException("obj"); if (index >= obj.Count) throw new ArgumentOutOfRangeException("index"); int i = 0; foreach (KeyValuePair o in obj) if (i++ == index) return o.Value; return null; } /// /// Adds the specified key. /// /// The key. /// The value. public void Add(string key, object value) { _members.Add(key, value); } /// /// Determines whether the specified key contains key. /// /// The key. /// /// true if the specified key contains key; otherwise, false. /// public bool ContainsKey(string key) { return _members.ContainsKey(key); } /// /// Gets the keys. /// /// The keys. public ICollection Keys { get { return _members.Keys; } } /// /// Removes the specified key. /// /// The key. /// public bool Remove(string key) { return _members.Remove(key); } /// /// Tries the get value. /// /// The key. /// The value. /// public bool TryGetValue(string key, out object value) { return _members.TryGetValue(key, out value); } /// /// Gets the values. /// /// The values. public ICollection Values { get { return _members.Values; } } /// /// Gets or sets the with the specified key. /// /// public object this[string key] { get { return _members[key]; } set { _members[key] = value; } } /// /// Adds the specified item. /// /// The item. public void Add(KeyValuePair item) { _members.Add(item.Key, item.Value); } /// /// Clears this instance. /// public void Clear() { _members.Clear(); } /// /// Determines whether [contains] [the specified item]. /// /// The item. /// /// true if [contains] [the specified item]; otherwise, false. /// public bool Contains(KeyValuePair item) { return _members.ContainsKey(item.Key) && _members[item.Key] == item.Value; } /// /// Copies to. /// /// The array. /// Index of the array. public void CopyTo(KeyValuePair[] array, int arrayIndex) { int num = Count; foreach (KeyValuePair kvp in this) { array[arrayIndex++] = kvp; if (--num <= 0) return; } } /// /// Gets the count. /// /// The count. public int Count { get { return _members.Count; } } /// /// Gets a value indicating whether this instance is read only. /// /// /// true if this instance is read only; otherwise, false. /// public bool IsReadOnly { get { return false; } } /// /// Removes the specified item. /// /// The item. /// public bool Remove(KeyValuePair item) { return _members.Remove(item.Key); } /// /// Gets the enumerator. /// /// public IEnumerator> GetEnumerator() { return _members.GetEnumerator(); } /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// IEnumerator IEnumerable.GetEnumerator() { return _members.GetEnumerator(); } /// /// Returns a json that represents the current . /// /// /// A json that represents the current . /// public override string ToString() { return SimpleJson.SerializeObject(this); } #if SIMPLE_JSON_DYNAMIC /// /// Provides implementation for type conversion operations. Classes derived from the class can override this method to specify dynamic behavior for operations that convert an object from one type to another. /// /// Provides information about the conversion operation. The binder.Type property provides the type to which the object must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where sampleObject is an instance of the class derived from the class, binder.Type returns the type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns true for explicit conversion and false for implicit conversion. /// The result of the type conversion operation. /// /// Alwasy returns true. /// public override bool TryConvert(ConvertBinder binder, out object result) { // if (binder == (ConvertBinder)null) throw new ArgumentNullException("binder"); // Type targetType = binder.Type; if ((targetType == typeof(IEnumerable)) || (targetType == typeof(IEnumerable>)) || (targetType == typeof(IDictionary)) || #if NETFX_CORE (targetType == typeof(IDictionary<,>)) #else (targetType == typeof(IDictionary)) #endif ) { result = this; return true; } return base.TryConvert(binder, out result); } /// /// Provides the implementation for operations that delete an object member. This method is not intended for use in C# or Visual Basic. /// /// Provides information about the deletion. /// /// Alwasy returns true. /// public override bool TryDeleteMember(DeleteMemberBinder binder) { // if (binder == (DeleteMemberBinder)null) throw new ArgumentNullException("binder"); // return _members.Remove(binder.Name); } /// /// Provides the implementation for operations that get a value by index. Classes derived from the class can override this method to specify dynamic behavior for indexing operations. /// /// Provides information about the operation. /// The indexes that are used in the operation. For example, for the sampleObject[3] operation in C# (sampleObject(3) in Visual Basic), where sampleObject is derived from the DynamicObject class, is equal to 3. /// The result of the index operation. /// /// Alwasy returns true. /// public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes.Length == 1) { result = ((IDictionary)this)[(string)indexes[0]]; return true; } result = (object)null; return true; } /// /// Provides the implementation for operations that get member values. Classes derived from the class can override this method to specify dynamic behavior for operations such as getting a value for a property. /// /// Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive. /// The result of the get operation. For example, if the method is called for a property, you can assign the property value to . /// /// Alwasy returns true. /// public override bool TryGetMember(GetMemberBinder binder, out object result) { object value; if (_members.TryGetValue(binder.Name, out value)) { result = value; return true; } result = (object)null; return true; } /// /// Provides the implementation for operations that set a value by index. Classes derived from the class can override this method to specify dynamic behavior for operations that access objects by a specified index. /// /// Provides information about the operation. /// The indexes that are used in the operation. For example, for the sampleObject[3] = 10 operation in C# (sampleObject(3) = 10 in Visual Basic), where sampleObject is derived from the class, is equal to 3. /// The value to set to the object that has the specified index. For example, for the sampleObject[3] = 10 operation in C# (sampleObject(3) = 10 in Visual Basic), where sampleObject is derived from the class, is equal to 10. /// /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown. /// public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) { if (indexes.Length == 1) { ((IDictionary)this)[(string)indexes[0]] = value; return true; } return base.TrySetIndex(binder, indexes, value); } /// /// Provides the implementation for operations that set member values. Classes derived from the class can override this method to specify dynamic behavior for operations such as setting a value for a property. /// /// Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member to which the value is being assigned. For example, for the statement sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive. /// The value to set to the member. For example, for sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the class, the is "Test". /// /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.) /// public override bool TrySetMember(SetMemberBinder binder, object value) { // if (binder == (SetMemberBinder)null) throw new ArgumentNullException("binder"); // _members[binder.Name] = value; return true; } /// /// Returns the enumeration of all dynamic member names. /// /// /// A sequence that contains dynamic member names. /// public override IEnumerable GetDynamicMemberNames() { foreach (var key in Keys) yield return key; } #endif } #endregion } namespace SimpleJson { #region JsonParser /// /// This class encodes and decodes JSON strings. /// Spec. details, see http://www.json.org/ /// /// JSON uses Arrays and Objects. These correspond here to the datatypes JsonArray(IList<object>) and JsonObject(IDictionary<string,object>). /// All numbers are parsed to doubles. /// #if SIMPLE_JSON_INTERNAL internal #else public #endif class SimpleJson { private const int TOKEN_NONE = 0; private const int TOKEN_CURLY_OPEN = 1; private const int TOKEN_CURLY_CLOSE = 2; private const int TOKEN_SQUARED_OPEN = 3; private const int TOKEN_SQUARED_CLOSE = 4; private const int TOKEN_COLON = 5; private const int TOKEN_COMMA = 6; private const int TOKEN_STRING = 7; private const int TOKEN_NUMBER = 8; private const int TOKEN_TRUE = 9; private const int TOKEN_FALSE = 10; private const int TOKEN_NULL = 11; private const int BUILDER_CAPACITY = 2000; /// /// Parses the string json into a value /// /// A JSON string. /// An IList<object>, a IDictionary<string,object>, a double, a string, null, true, or false public static object DeserializeObject(string json) { object @object; if (TryDeserializeObject(json, out @object)) return @object; throw new System.Runtime.Serialization.SerializationException("Invalid JSON string"); } /// /// Try parsing the json string into a value. /// /// /// A JSON string. /// /// /// The object. /// /// /// Returns true if successfull otherwise false. /// public static bool TryDeserializeObject(string json, out object @object) { bool success = true; if (json != null) { char[] charArray = json.ToCharArray(); int index = 0; @object = ParseValue(charArray, ref index, ref success); } else @object = null; return success; } public static object DeserializeObject(string json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) { object jsonObject = DeserializeObject(json); return type == null || jsonObject != null && #if NETFX_CORE jsonObject.GetType().GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) #else jsonObject.GetType().IsAssignableFrom(type) #endif ? jsonObject : (jsonSerializerStrategy ?? CurrentJsonSerializerStrategy).DeserializeObject(jsonObject, type); } public static object DeserializeObject(string json, Type type) { return DeserializeObject(json, type, null); } public static T DeserializeObject(string json, IJsonSerializerStrategy jsonSerializerStrategy) { return (T)DeserializeObject(json, typeof(T), jsonSerializerStrategy); } public static T DeserializeObject(string json) { return (T)DeserializeObject(json, typeof(T), null); } /// /// Converts a IDictionary<string,object> / IList<object> object into a JSON string /// /// A IDictionary<string,object> / IList<object> /// Serializer strategy to use /// A JSON encoded string, or null if object 'json' is not serializable public static string SerializeObject(object json, IJsonSerializerStrategy jsonSerializerStrategy) { StringBuilder builder = new StringBuilder(BUILDER_CAPACITY); bool success = SerializeValue(jsonSerializerStrategy, json, builder); return (success ? builder.ToString() : null); } public static string SerializeObject(object json) { return SerializeObject(json, CurrentJsonSerializerStrategy); } protected static IDictionary ParseObject(char[] json, ref int index, ref bool success) { IDictionary table = new JsonObject(); int token; // { NextToken(json, ref index); bool done = false; while (!done) { token = LookAhead(json, index); if (token == TOKEN_NONE) { success = false; return null; } else if (token == TOKEN_COMMA) NextToken(json, ref index); else if (token == TOKEN_CURLY_CLOSE) { NextToken(json, ref index); return table; } else { // name string name = ParseString(json, ref index, ref success); if (!success) { success = false; return null; } // : token = NextToken(json, ref index); if (token != TOKEN_COLON) { success = false; return null; } // value object value = ParseValue(json, ref index, ref success); if (!success) { success = false; return null; } table[name] = value; } } return table; } protected static JsonArray ParseArray(char[] json, ref int index, ref bool success) { JsonArray array = new JsonArray(); // [ NextToken(json, ref index); bool done = false; while (!done) { int token = LookAhead(json, index); if (token == TOKEN_NONE) { success = false; return null; } else if (token == TOKEN_COMMA) NextToken(json, ref index); else if (token == TOKEN_SQUARED_CLOSE) { NextToken(json, ref index); break; } else { object value = ParseValue(json, ref index, ref success); if (!success) return null; array.Add(value); } } return array; } protected static object ParseValue(char[] json, ref int index, ref bool success) { switch (LookAhead(json, index)) { case TOKEN_STRING: return ParseString(json, ref index, ref success); case TOKEN_NUMBER: return ParseNumber(json, ref index, ref success); case TOKEN_CURLY_OPEN: return ParseObject(json, ref index, ref success); case TOKEN_SQUARED_OPEN: return ParseArray(json, ref index, ref success); case TOKEN_TRUE: NextToken(json, ref index); return true; case TOKEN_FALSE: NextToken(json, ref index); return false; case TOKEN_NULL: NextToken(json, ref index); return null; case TOKEN_NONE: break; } success = false; return null; } protected static string ParseString(char[] json, ref int index, ref bool success) { StringBuilder s = new StringBuilder(BUILDER_CAPACITY); char c; EatWhitespace(json, ref index); // " c = json[index++]; bool complete = false; while (!complete) { if (index == json.Length) { break; } c = json[index++]; if (c == '"') { complete = true; break; } else if (c == '\\') { if (index == json.Length) break; c = json[index++]; if (c == '"') s.Append('"'); else if (c == '\\') s.Append('\\'); else if (c == '/') s.Append('/'); else if (c == 'b') s.Append('\b'); else if (c == 'f') s.Append('\f'); else if (c == 'n') s.Append('\n'); else if (c == 'r') s.Append('\r'); else if (c == 't') s.Append('\t'); else if (c == 'u') { int remainingLength = json.Length - index; if (remainingLength >= 4) { // parse the 32 bit hex into an integer codepoint uint codePoint; if ( !(success = UInt32.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out codePoint))) return ""; // convert the integer codepoint to a unicode char and add to string if (0xD800 <= codePoint && codePoint <= 0xDBFF) // if high surrogate { index += 4; // skip 4 chars remainingLength = json.Length - index; if (remainingLength >= 6) { uint lowCodePoint; if (new string(json, index, 2) == "\\u" && UInt32.TryParse(new string(json, index + 2, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out lowCodePoint)) { if (0xDC00 <= lowCodePoint && lowCodePoint <= 0xDFFF) // if low surrogate { s.Append((char)codePoint); s.Append((char)lowCodePoint); index += 6; // skip 6 chars continue; } } } success = false; // invalid surrogate pair return ""; } #if SILVERLIGHT s.Append(ConvertFromUtf32((int)codePoint)); #else s.Append(Char.ConvertFromUtf32((int)codePoint)); #endif // skip 4 chars index += 4; } else break; } } else s.Append(c); } if (!complete) { success = false; return null; } return s.ToString(); } #if SILVERLIGHT private static string ConvertFromUtf32(int utf32) { // http://www.java2s.com/Open-Source/CSharp/2.6.4-mono-.net-core/System/System/Char.cs.htm if (utf32 < 0 || utf32 > 0x10FFFF) throw new ArgumentOutOfRangeException("utf32", "The argument must be from 0 to 0x10FFFF."); if (0xD800 <= utf32 && utf32 <= 0xDFFF) throw new ArgumentOutOfRangeException("utf32", "The argument must not be in surrogate pair range."); if (utf32 < 0x10000) return new string((char)utf32, 1); utf32 -= 0x10000; return new string(new char[] {(char) ((utf32 >> 10) + 0xD800),(char) (utf32 % 0x0400 + 0xDC00)}); } #endif protected static object ParseNumber(char[] json, ref int index, ref bool success) { EatWhitespace(json, ref index); int lastIndex = GetLastIndexOfNumber(json, index); int charLength = (lastIndex - index) + 1; object returnNumber; string str = new string(json, index, charLength); if (str.IndexOf(".", StringComparison.OrdinalIgnoreCase) != -1 || str.IndexOf("e", StringComparison.OrdinalIgnoreCase) != -1) { double number; success = double.TryParse(new string(json, index, charLength), NumberStyles.Any, CultureInfo.InvariantCulture, out number); returnNumber = number; } else { long number; success = long.TryParse(new string(json, index, charLength), NumberStyles.Any, CultureInfo.InvariantCulture, out number); returnNumber = number; } index = lastIndex + 1; return returnNumber; } protected static int GetLastIndexOfNumber(char[] json, int index) { int lastIndex; for (lastIndex = index; lastIndex < json.Length; lastIndex++) if ("0123456789+-.eE".IndexOf(json[lastIndex]) == -1) break; return lastIndex - 1; } protected static void EatWhitespace(char[] json, ref int index) { for (; index < json.Length; index++) if (" \t\n\r\b\f".IndexOf(json[index]) == -1) break; } protected static int LookAhead(char[] json, int index) { int saveIndex = index; return NextToken(json, ref saveIndex); } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] protected static int NextToken(char[] json, ref int index) { EatWhitespace(json, ref index); if (index == json.Length) return TOKEN_NONE; char c = json[index]; index++; switch (c) { case '{': return TOKEN_CURLY_OPEN; case '}': return TOKEN_CURLY_CLOSE; case '[': return TOKEN_SQUARED_OPEN; case ']': return TOKEN_SQUARED_CLOSE; case ',': return TOKEN_COMMA; case '"': return TOKEN_STRING; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': return TOKEN_NUMBER; case ':': return TOKEN_COLON; } index--; int remainingLength = json.Length - index; // false if (remainingLength >= 5) { if (json[index] == 'f' && json[index + 1] == 'a' && json[index + 2] == 'l' && json[index + 3] == 's' && json[index + 4] == 'e') { index += 5; return TOKEN_FALSE; } } // true if (remainingLength >= 4) { if (json[index] == 't' && json[index + 1] == 'r' && json[index + 2] == 'u' && json[index + 3] == 'e') { index += 4; return TOKEN_TRUE; } } // null if (remainingLength >= 4) { if (json[index] == 'n' && json[index + 1] == 'u' && json[index + 2] == 'l' && json[index + 3] == 'l') { index += 4; return TOKEN_NULL; } } return TOKEN_NONE; } protected static string GetIndentString(int indent) { string ret = ""; for (int i = 0; i < indent; ++i) { ret += "\t"; } return ret; } protected static bool SerializeValue(IJsonSerializerStrategy jsonSerializerStrategy, object value, StringBuilder builder, int indent = 1) { bool success = true; if (value is string) success = SerializeString((string)value, builder); else if (value is IDictionary) { IDictionary dict = (IDictionary)value; success = SerializeObject(jsonSerializerStrategy, dict.Keys, dict.Values, builder, indent); } else if (value is IDictionary) { IDictionary dict = (IDictionary)value; success = SerializeObject(jsonSerializerStrategy, dict.Keys, dict.Values, builder, indent); } else if (value != null && ReflectionUtils.IsTypeDictionary(value.GetType())) { IDictionary dict = (IDictionary)value; success = SerializeObject(jsonSerializerStrategy, dict.Keys, dict.Values, builder, indent); } else if (value is IEnumerable) success = SerializeArray(jsonSerializerStrategy, (IEnumerable)value, builder, indent); else if (IsNumeric(value)) success = SerializeNumber(value, builder); else if (value is Boolean) builder.Append((bool)value ? "true" : "false"); else if (value == null) builder.Append("null"); else { object serializedObject; success = jsonSerializerStrategy.SerializeNonPrimitiveObject(value, out serializedObject); if (success) SerializeValue(jsonSerializerStrategy, serializedObject, builder, indent); } return success; } protected static bool SerializeObject(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable keys, IEnumerable values, StringBuilder builder, int indent) { builder.Append("{\r\n"); IEnumerator ke = keys.GetEnumerator(); IEnumerator ve = values.GetEnumerator(); bool first = true; while (ke.MoveNext() && ve.MoveNext()) { object key = ke.Current; object value = ve.Current; if (!first) { builder.Append(",\r\n"); } if (key is string) { builder.Append(GetIndentString(indent)); SerializeString((string)key, builder); } else if (!SerializeValue(jsonSerializerStrategy, value, builder, indent + 1)) return false; builder.Append(" : "); if (!SerializeValue(jsonSerializerStrategy, value, builder, indent + 1)) return false; first = false; } builder.Append("\r\n"); builder.Append(GetIndentString(indent - 1)); builder.Append("}"); return true; } protected static bool SerializeArray(IJsonSerializerStrategy jsonSerializerStrategy, IEnumerable anArray, StringBuilder builder, int indent) { builder.Append("[\r\n"); bool first = true; foreach (object value in anArray) { if (!first) { builder.Append(",\r\n"); } builder.Append(GetIndentString(indent)); if (!SerializeValue(jsonSerializerStrategy, value, builder, indent + 1)) return false; first = false; } builder.Append("\r\n"); builder.Append(GetIndentString(indent - 1)); builder.Append("]"); return true; } protected static bool SerializeString(string aString, StringBuilder builder) { builder.Append("\""); char[] charArray = aString.ToCharArray(); for (int i = 0; i < charArray.Length; i++) { char c = charArray[i]; if (c == '"') builder.Append("\\\""); else if (c == '\\') builder.Append("\\\\"); else if (c == '\b') builder.Append("\\b"); else if (c == '\f') builder.Append("\\f"); else if (c == '\n') builder.Append("\\n"); else if (c == '\r') builder.Append("\\r"); else if (c == '\t') builder.Append("\\t"); else builder.Append(c); } builder.Append("\""); return true; } protected static bool SerializeNumber(object number, StringBuilder builder) { if (number is long) { builder.Append(((long)number).ToString(CultureInfo.InvariantCulture)); } else if (number is ulong) { builder.Append(((ulong)number).ToString(CultureInfo.InvariantCulture)); } else if (number is int) { builder.Append(((int)number).ToString(CultureInfo.InvariantCulture)); } else if (number is uint) { builder.Append(((uint)number).ToString(CultureInfo.InvariantCulture)); } else if (number is decimal) { builder.Append(((decimal)number).ToString(CultureInfo.InvariantCulture)); } else if (number is float) { builder.Append(((float)number).ToString(CultureInfo.InvariantCulture)); } else { builder.Append(Convert.ToDouble(number, CultureInfo.InvariantCulture).ToString("r", CultureInfo.InvariantCulture)); } return true; } /// /// Determines if a given object is numeric in any way /// (can be integer, double, null, etc). /// protected static bool IsNumeric(object value) { if (value is sbyte) return true; if (value is byte) return true; if (value is short) return true; if (value is ushort) return true; if (value is int) return true; if (value is uint) return true; if (value is long) return true; if (value is ulong) return true; if (value is float) return true; if (value is double) return true; if (value is decimal) return true; return false; } private static IJsonSerializerStrategy currentJsonSerializerStrategy; public static IJsonSerializerStrategy CurrentJsonSerializerStrategy { get { // todo: implement locking mechanism. return currentJsonSerializerStrategy ?? (currentJsonSerializerStrategy = #if SIMPLE_JSON_DATACONTRACT DataContractJsonSerializerStrategy #else PocoJsonSerializerStrategy #endif ); } set { currentJsonSerializerStrategy = value; } } private static PocoJsonSerializerStrategy pocoJsonSerializerStrategy; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)] public static PocoJsonSerializerStrategy PocoJsonSerializerStrategy { get { // todo: implement locking mechanism. return pocoJsonSerializerStrategy ?? (pocoJsonSerializerStrategy = new PocoJsonSerializerStrategy()); } } #if SIMPLE_JSON_DATACONTRACT private static DataContractJsonSerializerStrategy dataContractJsonSerializerStrategy; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)] public static DataContractJsonSerializerStrategy DataContractJsonSerializerStrategy { get { // todo: implement locking mechanism. return dataContractJsonSerializerStrategy ?? (dataContractJsonSerializerStrategy = new DataContractJsonSerializerStrategy()); } } #endif } #endregion #region Simple Json Serializer Strategies #if SIMPLE_JSON_INTERNAL internal #else public #endif interface IJsonSerializerStrategy { bool SerializeNonPrimitiveObject(object input, out object output); object DeserializeObject(object value, Type type); } #if SIMPLE_JSON_INTERNAL internal #else public #endif class PocoJsonSerializerStrategy : IJsonSerializerStrategy { internal CacheResolver CacheResolver; private static readonly string[] Iso8601Format = new string[] { @"yyyy-MM-dd\THH:mm:ss.FFFFFFF\Z", @"yyyy-MM-dd\THH:mm:ss\Z", @"yyyy-MM-dd\THH:mm:ssK" }; public PocoJsonSerializerStrategy() { CacheResolver = new CacheResolver(BuildMap); } protected virtual void BuildMap(Type type, SafeDictionary memberMaps) { #if NETFX_CORE foreach (PropertyInfo info in type.GetTypeInfo().DeclaredProperties) { var getMethod = info.GetMethod; if(getMethod==null || !getMethod.IsPublic || getMethod.IsStatic) continue; #else foreach (PropertyInfo info in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { #endif memberMaps.Add(info.Name, new CacheResolver.MemberMap(info)); } #if NETFX_CORE foreach (FieldInfo info in type.GetTypeInfo().DeclaredFields) { if(!info.IsPublic || info.IsStatic) continue; #else foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) { #endif memberMaps.Add(info.Name, new CacheResolver.MemberMap(info)); } } public virtual bool SerializeNonPrimitiveObject(object input, out object output) { return TrySerializeKnownTypes(input, out output) || TrySerializeUnknownTypes(input, out output); } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] public virtual object DeserializeObject(object value, Type type) { object obj = null; if (value is string) { string str = value as string; if (!string.IsNullOrEmpty(str)) { if (type == typeof(DateTime) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(DateTime))) obj = DateTime.ParseExact(str, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); else if (type == typeof(Guid) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid))) obj = new Guid(str); else if (type == typeof(Font) || (ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Font))) { var cvt = new FontConverter(); obj = cvt.ConvertFromInvariantString(str); } else obj = str; } else { if (type == typeof(Guid)) obj= default(Guid); else if(ReflectionUtils.IsNullableType(type) && Nullable.GetUnderlyingType(type) == typeof(Guid)) obj = null; else obj = str; } } else if (value is bool) obj = value; else if (value == null) obj = null; else if (value is long && typeof(Enum).IsAssignableFrom(type)) { obj = Enum.ToObject(type, (long) value); } else if ((value is long && type == typeof(long)) || (value is double && type == typeof(double))) obj = value; else if ((value is double && type != typeof(double)) || (value is long && type != typeof(long))) { obj = #if NETFX_CORE type == typeof(int) || type == typeof(long) || type == typeof(double) ||type == typeof(float) || type == typeof(bool) || type == typeof(decimal) ||type == typeof(byte) || type == typeof(short) #else typeof(IConvertible).IsAssignableFrom(type) #endif ? Convert.ChangeType(value, type, CultureInfo.InvariantCulture) : value; } else { if (value is IDictionary) { IDictionary jsonObject = (IDictionary)value; if (ReflectionUtils.IsTypeDictionary(type)) { // if dictionary then #if NETFX_CORE Type keyType = type.GetTypeInfo().GenericTypeArguments[0]; Type valueType = type.GetTypeInfo().GenericTypeArguments[1]; #else Type keyType = type.GetGenericArguments()[0]; Type valueType = type.GetGenericArguments()[1]; #endif Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); #if NETFX_CORE dynamic dict = CacheResolver.GetNewInstance(genericType); #else IDictionary dict = (IDictionary)CacheResolver.GetNewInstance(genericType); #endif foreach (KeyValuePair kvp in jsonObject) { dict.Add(kvp.Key, DeserializeObject(kvp.Value, valueType)); } obj = dict; } else { obj = CacheResolver.GetNewInstance(type); SafeDictionary maps = CacheResolver.LoadMaps(type); if (maps == null) { obj = value; } else { foreach (KeyValuePair keyValuePair in maps) { CacheResolver.MemberMap v = keyValuePair.Value; if (v.Setter == null) continue; string jsonKey = keyValuePair.Key; if (jsonObject.ContainsKey(jsonKey)) { object jsonValue = DeserializeObject(jsonObject[jsonKey], v.Type); v.Setter(obj, jsonValue); } } } } } else if (value is IList) { IList jsonObject = (IList)value; IList list = null; if (type.IsArray) { list = (IList)Activator.CreateInstance(type, jsonObject.Count); int i = 0; foreach (object o in jsonObject) list[i++] = DeserializeObject(o, type.GetElementType()); } else if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || #if NETFX_CORE typeof(IList).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) #else typeof(IList).IsAssignableFrom(type) #endif ) { #if NETFX_CORE Type innerType = type.GetTypeInfo().GenericTypeArguments[0]; #else Type innerType = type.GetGenericArguments()[0]; #endif Type genericType = typeof(List<>).MakeGenericType(innerType); list = (IList)CacheResolver.GetNewInstance(genericType); foreach (object o in jsonObject) list.Add(DeserializeObject(o, innerType)); } obj = list; } return obj; } if (ReflectionUtils.IsNullableType(type)) return ReflectionUtils.ToNullableType(obj, type); if (obj == null) { if (type == typeof(Guid)) return default(Guid); } return obj; } protected virtual object SerializeEnum(Enum p) { return Convert.ToDouble(p, CultureInfo.InvariantCulture); } protected virtual bool TrySerializeKnownTypes(object input, out object output) { bool returnValue = true; if (input is DateTime) output = ((DateTime)input).ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture); else if (input is Guid) output = ((Guid)input).ToString("D"); else if (input is Uri) output = input.ToString(); else if (input is Enum) output = SerializeEnum((Enum)input); else if (input is Font) { var cvt = new FontConverter(); output = cvt.ConvertToInvariantString((Font) input); } else { returnValue = false; output = null; } return returnValue; } protected virtual bool TrySerializeUnknownTypes(object input, out object output) { output = null; // todo: implement caching for types Type type = input.GetType(); if (type.FullName == null) return false; IDictionary obj = new JsonObject(); SafeDictionary maps = CacheResolver.LoadMaps(type); foreach (KeyValuePair keyValuePair in maps) { if (keyValuePair.Value.Getter != null) obj.Add(keyValuePair.Key, keyValuePair.Value.Getter(input)); } output = obj; return true; } } #if SIMPLE_JSON_DATACONTRACT #if SIMPLE_JSON_INTERNAL internal #else public #endif class DataContractJsonSerializerStrategy : PocoJsonSerializerStrategy { public DataContractJsonSerializerStrategy() { CacheResolver = new CacheResolver(BuildMap); } protected override void BuildMap(Type type, SafeDictionary map) { bool hasDataContract = ReflectionUtils.GetAttribute(type, typeof(DataContractAttribute)) != null; if (!hasDataContract) { base.BuildMap(type, map); return; } string jsonKey; #if NETFX_CORE foreach (PropertyInfo info in type.GetTypeInfo().DeclaredProperties) #else foreach (PropertyInfo info in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) #endif { if (CanAdd(info, out jsonKey)) map.Add(jsonKey, new CacheResolver.MemberMap(info)); } #if NETFX_CORE foreach (FieldInfo info in type.GetTypeInfo().DeclaredFields) #else foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) #endif { if (CanAdd(info, out jsonKey)) map.Add(jsonKey, new CacheResolver.MemberMap(info)); } // todo implement sorting for DATACONTRACT. } private static bool CanAdd(MemberInfo info, out string jsonKey) { jsonKey = null; if (ReflectionUtils.GetAttribute(info, typeof(IgnoreDataMemberAttribute)) != null) return false; DataMemberAttribute dataMemberAttribute = (DataMemberAttribute)ReflectionUtils.GetAttribute(info, typeof(DataMemberAttribute)); if (dataMemberAttribute == null) return false; jsonKey = string.IsNullOrEmpty(dataMemberAttribute.Name) ? info.Name : dataMemberAttribute.Name; return true; } } #endif #endregion #region Reflection helpers namespace Reflection { #if SIMPLE_JSON_INTERNAL internal #else public #endif class ReflectionUtils { public static Attribute GetAttribute(MemberInfo info, Type type) { #if NETFX_CORE if (info == null || type == null || !info.IsDefined(type)) return null; return info.GetCustomAttribute(type); #else if (info == null || type == null || !Attribute.IsDefined(info, type)) return null; return Attribute.GetCustomAttribute(info, type); #endif } public static Attribute GetAttribute(Type objectType, Type attributeType) { #if NETFX_CORE if (objectType == null || attributeType == null || !objectType.GetTypeInfo().IsDefined(attributeType)) return null; return objectType.GetTypeInfo().GetCustomAttribute(attributeType); #else if (objectType == null || attributeType == null || !Attribute.IsDefined(objectType, attributeType)) return null; return Attribute.GetCustomAttribute(objectType, attributeType); #endif } public static bool IsTypeGenericeCollectionInterface(Type type) { #if NETFX_CORE if (!type.GetTypeInfo().IsGenericType) #else if (!type.IsGenericType) #endif return false; Type genericDefinition = type.GetGenericTypeDefinition(); return (genericDefinition == typeof(IList<>) || genericDefinition == typeof(ICollection<>) || genericDefinition == typeof(IEnumerable<>)); } public static bool IsTypeDictionary(Type type) { #if NETFX_CORE if (typeof(IDictionary<,>).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo())) return true; if (!type.GetTypeInfo().IsGenericType) return false; #else if (typeof(IDictionary).IsAssignableFrom(type)) return true; if (!type.IsGenericType) return false; #endif Type genericDefinition = type.GetGenericTypeDefinition(); return genericDefinition == typeof(IDictionary<,>); } public static bool IsNullableType(Type type) { return #if NETFX_CORE type.GetTypeInfo().IsGenericType #else type.IsGenericType #endif && type.GetGenericTypeDefinition() == typeof(Nullable<>); } public static object ToNullableType(object obj, Type nullableType) { return obj == null ? null : Convert.ChangeType(obj, Nullable.GetUnderlyingType(nullableType), CultureInfo.InvariantCulture); } } #if SIMPLE_JSON_INTERNAL internal #else public #endif delegate object GetHandler(object source); #if SIMPLE_JSON_INTERNAL internal #else public #endif delegate void SetHandler(object source, object value); #if SIMPLE_JSON_INTERNAL internal #else public #endif delegate void MemberMapLoader(Type type, SafeDictionary memberMaps); #if SIMPLE_JSON_INTERNAL internal #else public #endif class CacheResolver { private readonly MemberMapLoader _memberMapLoader; private readonly SafeDictionary> _memberMapsCache = new SafeDictionary>(); delegate object CtorDelegate(); readonly static SafeDictionary ConstructorCache = new SafeDictionary(); public CacheResolver(MemberMapLoader memberMapLoader) { _memberMapLoader = memberMapLoader; } [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")] public static object GetNewInstance(Type type) { CtorDelegate c; if (ConstructorCache.TryGetValue(type, out c)) return c(); #if SIMPLE_JSON_REFLECTIONEMIT DynamicMethod dynamicMethod = new DynamicMethod("Create" + type.FullName, typeof(object), Type.EmptyTypes, type, true); dynamicMethod.InitLocals = true; ILGenerator generator = dynamicMethod.GetILGenerator(); if (type.IsValueType) { generator.DeclareLocal(type); generator.Emit(OpCodes.Ldloc_0); generator.Emit(OpCodes.Box, type); } else { ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (constructorInfo == null) throw new Exception(string.Format(CultureInfo.InvariantCulture, "Could not get constructor for {0}.", type)); generator.Emit(OpCodes.Newobj, constructorInfo); } generator.Emit(OpCodes.Ret); c = (CtorDelegate)dynamicMethod.CreateDelegate(typeof(CtorDelegate)); ConstructorCache.Add(type, c); return c(); #else #if NETFX_CORE IEnumerable constructorInfos = type.GetTypeInfo().DeclaredConstructors; ConstructorInfo constructorInfo = null; foreach (ConstructorInfo item in constructorInfos) // FirstOrDefault() { if (item.GetParameters().Length == 0) // Default ctor - make sure it doesn't contain any parameters { constructorInfo = item; break; } } #else ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); #endif c = delegate { return constructorInfo.Invoke(null); }; ConstructorCache.Add(type, c); return c(); #endif } public SafeDictionary LoadMaps(Type type) { if (type == null || type == typeof(object)) return null; SafeDictionary maps; if (_memberMapsCache.TryGetValue(type, out maps)) return maps; maps = new SafeDictionary(); _memberMapLoader(type, maps); _memberMapsCache.Add(type, maps); return maps; } #if SIMPLE_JSON_REFLECTIONEMIT static DynamicMethod CreateDynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner) { DynamicMethod dynamicMethod = !owner.IsInterface ? new DynamicMethod(name, returnType, parameterTypes, owner, true) : new DynamicMethod(name, returnType, parameterTypes, (Module)null, true); return dynamicMethod; } #endif static GetHandler CreateGetHandler(FieldInfo fieldInfo) { #if SIMPLE_JSON_REFLECTIONEMIT Type type = fieldInfo.FieldType; DynamicMethod dynamicGet = CreateDynamicMethod("Get" + fieldInfo.Name, fieldInfo.DeclaringType, new Type[] { typeof(object) }, fieldInfo.DeclaringType); ILGenerator getGenerator = dynamicGet.GetILGenerator(); getGenerator.Emit(OpCodes.Ldarg_0); getGenerator.Emit(OpCodes.Ldfld, fieldInfo); if (type.IsValueType) getGenerator.Emit(OpCodes.Box, type); getGenerator.Emit(OpCodes.Ret); return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler)); #else return delegate(object instance) { return fieldInfo.GetValue(instance); }; #endif } static SetHandler CreateSetHandler(FieldInfo fieldInfo) { if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral) return null; #if SIMPLE_JSON_REFLECTIONEMIT Type type = fieldInfo.FieldType; DynamicMethod dynamicSet = CreateDynamicMethod("Set" + fieldInfo.Name, null, new Type[] { typeof(object), typeof(object) }, fieldInfo.DeclaringType); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); if (type.IsValueType) setGenerator.Emit(OpCodes.Unbox_Any, type); setGenerator.Emit(OpCodes.Stfld, fieldInfo); setGenerator.Emit(OpCodes.Ret); return (SetHandler)dynamicSet.CreateDelegate(typeof(SetHandler)); #else return delegate(object instance, object value) { fieldInfo.SetValue(instance, value); }; #endif } static GetHandler CreateGetHandler(PropertyInfo propertyInfo) { #if NETFX_CORE MethodInfo getMethodInfo = propertyInfo.GetMethod; #else MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true); #endif if (getMethodInfo == null) return null; #if SIMPLE_JSON_REFLECTIONEMIT Type type = propertyInfo.PropertyType; DynamicMethod dynamicGet = CreateDynamicMethod("Get" + propertyInfo.Name, propertyInfo.DeclaringType, new Type[] { typeof(object) }, propertyInfo.DeclaringType); ILGenerator getGenerator = dynamicGet.GetILGenerator(); getGenerator.Emit(OpCodes.Ldarg_0); getGenerator.Emit(OpCodes.Call, getMethodInfo); if (type.IsValueType) getGenerator.Emit(OpCodes.Box, type); getGenerator.Emit(OpCodes.Ret); return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler)); #else #if NETFX_CORE return delegate(object instance) { return getMethodInfo.Invoke(instance, new Type[] { }); }; #else return delegate(object instance) { return getMethodInfo.Invoke(instance, Type.EmptyTypes); }; #endif #endif } static SetHandler CreateSetHandler(PropertyInfo propertyInfo) { #if NETFX_CORE MethodInfo setMethodInfo = propertyInfo.SetMethod; #else MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true); #endif if (setMethodInfo == null) return null; #if SIMPLE_JSON_REFLECTIONEMIT Type type = propertyInfo.PropertyType; DynamicMethod dynamicSet = CreateDynamicMethod("Set" + propertyInfo.Name, null, new Type[] { typeof(object), typeof(object) }, propertyInfo.DeclaringType); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); if (type.IsValueType) setGenerator.Emit(OpCodes.Unbox_Any, type); setGenerator.Emit(OpCodes.Call, setMethodInfo); setGenerator.Emit(OpCodes.Ret); return (SetHandler)dynamicSet.CreateDelegate(typeof(SetHandler)); #else return delegate(object instance, object value) { setMethodInfo.Invoke(instance, new[] { value }); }; #endif } #if SIMPLE_JSON_INTERNAL internal #else public #endif sealed class MemberMap { public readonly MemberInfo MemberInfo; public readonly Type Type; public readonly GetHandler Getter; public readonly SetHandler Setter; public MemberMap(PropertyInfo propertyInfo) { MemberInfo = propertyInfo; Type = propertyInfo.PropertyType; Getter = CreateGetHandler(propertyInfo); Setter = CreateSetHandler(propertyInfo); } public MemberMap(FieldInfo fieldInfo) { MemberInfo = fieldInfo; Type = fieldInfo.FieldType; Getter = CreateGetHandler(fieldInfo); Setter = CreateSetHandler(fieldInfo); } } } #if SIMPLE_JSON_INTERNAL internal #else public #endif class SafeDictionary { private readonly object _padlock = new object(); private readonly Dictionary _dictionary = new Dictionary(); public bool TryGetValue(TKey key, out TValue value) { return _dictionary.TryGetValue(key, out value); } public TValue this[TKey key] { get { return _dictionary[key]; } } public IEnumerator> GetEnumerator() { return ((ICollection>)_dictionary).GetEnumerator(); } public void Add(TKey key, TValue value) { lock (_padlock) { if (_dictionary.ContainsKey(key) == false) _dictionary.Add(key, value); } } } } #endregion } ================================================ FILE: shadowsocks-csharp/3rd/opendns/DnsQuery.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Net; using System.IO; using System.Text; using System.Net.Sockets; using System.Collections; using System.Diagnostics; //using System.Management; namespace OpenDNS { /// /// DnsQuery Class /// Handles the dns message transport and interpretation of result. /// Use Send() to activate Result object. /// public class DnsQuery { //consider killing private byte[] data; private int position, length; //Question public string Domain; public Types QueryType; public Classes QueryClass; public int Port; public ArrayList Servers; public bool RecursionDesired; //Internal Read-Only private DnsResponse _Response; //Public Properties public DnsResponse Response { get { return _Response; } } /// /// Default Constructor with QueryType: A /// public DnsQuery() { Port = 53; Servers = new ArrayList(); QueryType = Types.A; QueryClass = Classes.IN; } public DnsQuery(string _Domain, Types _Type) { Port = 53; Servers = new ArrayList(); QueryType = _Type; Domain = _Domain; QueryClass = Classes.IN; RecursionDesired = true; } /// /// Transmit message to each DNS servers /// until one returns a response object. /// /// True if response object is ready public bool Send() { CheckForServers(); foreach (IPEndPoint Server in Servers) { int port = Server.Port; try { SendQuery2(Server.Address, port); break; } catch { continue; } } return (this.Response != null); } /// /// Uses UDPClient to send byte array to /// DNS Server Specified /// /// Target DNS Server private void SendQuery(string ipAddress) { if (ipAddress == null) throw new ArgumentNullException(); //opening the UDP socket at DNS server UdpClient dnsClient = new UdpClient(ipAddress, Port); //preparing the DNS query packet. byte[] QueryPacket = MakeQuery(); //send the data packet dnsClient.Send(QueryPacket, QueryPacket.Length); IPEndPoint endpoint = null; //receive the data packet from DNS server data = dnsClient.Receive(ref endpoint); length = data.Length; //un pack the byte array & makes an array of resource record objects. ReadResponse(); //kill dns dnsClient.Close(); } private void SendQuery2(IPAddress ipAddress, int port) { int timeout = 5000; if (ipAddress == null) throw new ArgumentNullException(); //preparing the DNS query packet. byte[] QueryPacket = MakeQuery(); //opening the UDP socket at DNS server IPAddress serverAddress = ipAddress; EndPoint endPoint = new IPEndPoint(serverAddress, port); Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeout); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout); socket.SendTo(QueryPacket, endPoint); data = new byte[512]; length = socket.ReceiveFrom(data, ref endPoint); //un pack the byte array & makes an array of resource record objects. ReadResponse(); socket.Shutdown(SocketShutdown.Both); } /// /// Packs question into byte array format /// accepted by DNS servers /// /// Byte Array to Send private byte[] MakeQuery() { //Get New ID int QueryID = new Random().Next(55555); //Initialize Packet Byte Array byte[] Question = new byte[512]; for (int i = 0; i < 512; i++) Question[i] = 0; ////Fill Packet Header //SetID Question[0] = (byte)(QueryID >> 8); Question[1] = (byte)(QueryID & byte.MaxValue); Question[2] = (byte)1; //Set OpCode to Regular Query //Set bool bit for recursion desired Question[2] = (byte)((this.RecursionDesired) ? (Question[2] | 1) : (Question[2] & 254)); //Set Recursion Available (Filler) Question[3] = (byte)0; //Set Question Count Question[4] = (byte)0; Question[5] = (byte)1; ///Fill Question Section //Set Domain Name to Query string[] tokens = this.Domain.Split(new char[] { '.' }); string label; int Cursor = 12; for (int j = 0; j < tokens.Length; j++) { //Get domain segment label = tokens[j]; //Set Length label for domain segment Question[Cursor++] = (byte)(label.Length & byte.MaxValue); //Get byte array of segment byte[] b = Encoding.ASCII.GetBytes(label); //Transcribe array into packet for (int k = 0; k < b.Length; k++) { Question[Cursor++] = b[k]; } } //End Domain Marker Question[Cursor++] = (byte)0; //Set Query type Question[Cursor++] = (byte)0; Question[Cursor++] = (byte)QueryType; //Set Query class Question[Cursor++] = (byte)0; Question[Cursor++] = (byte)QueryClass; return Question; } //for un packing the byte array private void ReadResponse() { ///////////////// //HEADER int ID = ((data[0] & byte.MaxValue) << 8) + (data[1] & byte.MaxValue); bool IS = (data[2] & 128) == 128; int OpCode = (data[2] >> 3 & 15); bool AA = (data[2] & 4) == 4; bool TC = (data[2] & 2) == 2; bool RD = (data[2] & 1) == 1; bool RA = (data[3] & 128) == 128; int Z = (data[3] & 1);//reserved, not used int RC = (data[3] & 15); //Counts int QuestionCount = ((data[4] & byte.MaxValue) << 8) | (data[5] & byte.MaxValue); int AnswerCount = ((data[6] & byte.MaxValue) << 8) | (data[7] & byte.MaxValue); //Trace.WriteLine("Answer count: " + AnswerCount); int AuthorityCount = ((data[8] & byte.MaxValue) << 8) | (data[9] & byte.MaxValue); int AdditionalCount = ((data[10] & byte.MaxValue) << 8) | (data[11] & byte.MaxValue); //Create Response Object this._Response = new DnsResponse(ID, AA, TC, RD, RA, RC); //FINISHED HEADER //GET QUESTIONS position = 12; for (int i = 0; i < QuestionCount; ++i) { string QuestionName = GetName(); //two octec field int TypeID = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; Types QuestionType = (Types)TypeID; //two octec field int ClassID = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; Classes QuestionClass = (Classes)ClassID; } for (int i = 0; i < AnswerCount; ++i) GetResourceRecord(i, _Response.Answers); for (int i = 0; i < AuthorityCount; ++i) GetResourceRecord(i, _Response.Authorities); for (int i = 0; i < AdditionalCount; ++i) GetResourceRecord(i, _Response.AdditionalRecords); } private void GetResourceRecord(int i, ResourceRecordCollection Container) { //get resource (answer) name string ResourceName = GetName(); //get resource type and class, usefull when using the ANY query //type: two octec field int TypeID = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; Types ResourceType = (Types)TypeID; //type: two octec field int ClassID = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; Classes ResourceClass = (Classes)ClassID; //ttl: unsigned integer int TTL_Seconds = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; //Get Resource Data Length int RDLength = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; //Parse Resource Data: 4 possible formats: A, Text, SOA and MX switch (ResourceType) { case Types.A: //Get IP Address Blocks { byte[] bs = new byte[] { (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue) }; string ResourceAddress = String.Concat(new object[] { bs[0], ".", bs[1], ".", bs[2], ".", bs[3] }); OpenDNS.Address rrA = new Address(ResourceName, ResourceType, ResourceClass, TTL_Seconds, ResourceAddress); Container.Add(rrA); break; } case Types.AAAA: { //Get IP Address Blocks ushort[] bs = new ushort[8]; for (int j = 0; j < 8; ++j) bs[j] = (ushort)(((byte)(data[position + j * 2] & byte.MaxValue) << 8) | (byte)(data[position + j * 2 + 1] & byte.MaxValue)); position += 16; string ResourceAddress = String.Concat(new object[] { Convert.ToString(bs[0], 16), ":", Convert.ToString(bs[1], 16), ":", Convert.ToString(bs[2], 16), ":", Convert.ToString(bs[3], 16), ":", Convert.ToString(bs[4], 16), ":", Convert.ToString(bs[5], 16), ":", Convert.ToString(bs[6], 16), ":", Convert.ToString(bs[7], 16)}); OpenDNS.Address rrA = new Address(ResourceName, ResourceType, ResourceClass, TTL_Seconds, ResourceAddress); Container.Add(rrA); break; } case Types.SOA: { //Extract Text Fields string Server = GetName(); string Email = GetName(); //32 bit fields long Serial = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; long Refresh = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; long Retry = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; long Expire = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; long Minimum = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue; OpenDNS.SOA rrSOA = new SOA(ResourceName, ResourceType, ResourceClass, TTL_Seconds, Server, Email, Serial, Refresh, Retry, Expire, Minimum); Container.Add(rrSOA); break; } case Types.CNAME: case Types.MINFO: case Types.NS: case Types.PTR: case Types.TXT: //Simplest RDATA format, just a text string, shared by many string ResourceDataText = GetName(); ResourceRecord rrTXT = new ResourceRecord(ResourceName, ResourceType, ResourceClass, TTL_Seconds, ResourceDataText); Container.Add(rrTXT); break; case Types.MX: int Rank = (data[position++] << 8) | (data[position++] & byte.MaxValue); string Exchange = GetName(); MX rrMX = new MX(ResourceName, ResourceType, ResourceClass, TTL_Seconds, Rank, Exchange); Container.Add(rrMX); break; default: Trace.WriteLine("Resource type did not match: " + ResourceType.ToString(), "RUY QDNS"); break; } } /// /// Wrapper for not so pretty code =) /// /// private string GetName() { StringBuilder sb = new StringBuilder(); position = ExtractName(position, sb); return sb.ToString(); } /// /// Gets name string segments from byte array. /// Uses the DNS "compression" support /// that gives a pointer to a previous /// occurrence of repeat names. /// -- not so pretty, consider killing /// /// Current Byte Array Reading Position /// New Global Cursor Position private int ExtractName(int ResourceDataCursor, StringBuilder Name) { //Get label for how many characters to extract in this segment int LengthLabel = (data[ResourceDataCursor++] & byte.MaxValue); if (LengthLabel == 0) { return ResourceDataCursor; } do { if ((LengthLabel & 0xC0) == 0xC0) { if (ResourceDataCursor >= length) { return -1; } //Compression OffsetID for RDATA Compression int CompressionOffsetID = ((LengthLabel & 0x3F) << 8) | (data[ResourceDataCursor++] & byte.MaxValue); ExtractName(CompressionOffsetID, Name); return ResourceDataCursor; } else { if ((ResourceDataCursor + LengthLabel) > length) { return -1; } Name.Append(Encoding.ASCII.GetString(data, ResourceDataCursor, LengthLabel)); ResourceDataCursor += LengthLabel; } if (ResourceDataCursor > length) { return -1; } LengthLabel = data[ResourceDataCursor++] & byte.MaxValue; //if new length label is larger than 0, we have another segment //so append dot. if (LengthLabel != 0) { Name.Append("."); } } while (LengthLabel != 0); return ResourceDataCursor; } /// /// Checks for any DNS servers /// on the public collection. If user /// did not add any manually gets /// the default ones from the TCP/IP /// Configuration. /// /// True if we have at least one DNS server private bool CheckForServers() { //Check if user added servers if (Servers.Count == 0) Servers = GetDefaultServers(); if (Servers.Count > 0) { return true; } else { throw new Exception("Abort: No DNS servers specified manually and could not get default ones."); //return false; } } /// /// TODO: /// Gets DNS Servers from TCP/IP Configuration of /// network adapter. /// /// ArrayList object. May be empty if no servers are found. private ArrayList GetDefaultServers() { ArrayList LocalServers = new ArrayList(); try { //Insert code here to query network adapter. } catch (Exception Ex) { Trace.WriteLine("Could not get DNS servers from network adapter: " + Ex.Message, "OpenDNS"); } finally { } return LocalServers; } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/DnsResponse.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Net; using System.Text; using System.Collections; using System.Diagnostics; namespace OpenDNS { /// /// Response object as result of a dns query message. /// Will be null unless query succesfull. /// public class DnsResponse { private int _QueryID; //Property Internals private bool _AuthorativeAnswer; private bool _IsTruncated; private bool _RecursionDesired; private bool _RecursionAvailable; private ResponseCodes _ResponseCode; private ResourceRecordCollection _ResourceRecords; private ResourceRecordCollection _Answers; private ResourceRecordCollection _Authorities; private ResourceRecordCollection _AdditionalRecords; //Read Only Public Properties public int QueryID { get { return _QueryID; } } public bool AuthorativeAnswer { get { return _AuthorativeAnswer; } } public bool IsTruncated { get { return _IsTruncated; } } public bool RecursionRequested { get { return _RecursionDesired; } } public bool RecursionAvailable { get { return _RecursionAvailable; } } public ResponseCodes ResponseCode { get { return _ResponseCode; } } public ResourceRecordCollection Answers { get { return _Answers; } } public ResourceRecordCollection Authorities { get { return _Authorities; } } public ResourceRecordCollection AdditionalRecords { get { return _AdditionalRecords; } } /// /// Unified collection of Resource Records from Answers, /// Authorities and Additional. NOT IN REALTIME SYNC. /// /// public ResourceRecordCollection ResourceRecords { get { if (_ResourceRecords.Count == 0 && _Answers.Count > 0 && _Authorities.Count > 0 && _AdditionalRecords.Count > 0) { foreach (ResourceRecord rr in Answers) this._ResourceRecords.Add(rr); foreach (ResourceRecord rr in Authorities) this._ResourceRecords.Add(rr); foreach (ResourceRecord rr in AdditionalRecords) this._ResourceRecords.Add(rr); } return _ResourceRecords; } } public DnsResponse(int ID, bool AA, bool TC, bool RD, bool RA, int RC) { this._QueryID = ID; this._AuthorativeAnswer = AA; this._IsTruncated = TC; this._RecursionDesired = RD; this._RecursionAvailable = RA; this._ResponseCode = (ResponseCodes)RC; this._ResourceRecords = new ResourceRecordCollection(); this._Answers = new ResourceRecordCollection(); this._Authorities = new ResourceRecordCollection(); this._AdditionalRecords = new ResourceRecordCollection(); } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/Enum.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; namespace OpenDNS { /// /// Query Result/Response Codes from server /// public enum ResponseCodes : int { NoError = 0, FormatError = 1, ServerFailure = 2, NameError = 3, NotImplemented = 4, Refused = 5, Reserved = 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 } /// /// DNS Resource Record Types /// public enum Types : int { A = 1, AAAA = 28, NS = 2, CNAME = 5, SOA = 6, MB = 7, MG = 8, MR = 9, NULL = 10, WKS = 11, PTR = 12, HINFO = 13, MINFO = 14, MX = 15, TXT = 16, ANY = 255 } /// /// Query Class or Scope /// public enum Classes : int { IN = 1, CS = 2, CH = 3, HS = 4, ANY = 255 } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/RR/Address.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Net; namespace OpenDNS { /// /// Address Resource Record /// public class Address : ResourceRecord { public string ResourceAddress; private IPAddress _IP; public IPAddress IP { get { if (_IP == null) _IP = IPAddress.Parse(ResourceAddress); return _IP; } } public Address(string _Name, Types _Type, Classes _Class, int _TimeToLive, string _ResourceAddress):base(_Name, _Type, _Class, _TimeToLive) { ResourceAddress = _ResourceAddress; RText = _ResourceAddress; } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/RR/MX.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Text; namespace OpenDNS { /// /// MailExchange Resource Record /// public class MX : ResourceRecord { public int Preference; public string Exchange; public MX(string _Name, Types _Type, Classes _Class, int _TimeToLive, int _Preference, string _Exchange):base(_Name, _Type, _Class, _TimeToLive) { Preference = _Preference; Exchange = _Exchange; } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/RR/ResourceRecord.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Text; namespace OpenDNS { /// /// Base Resource Record class for objects returned in /// answers, authorities and additional record DNS responses. /// public class ResourceRecord { public string Name; public Types Type; public Classes Class; public int TimeToLive; public string RText; public ResourceRecord() { } public ResourceRecord(string _Name, Types _Type, Classes _Class, int _TimeToLive) { this.Name = _Name; this.Type = _Type; this.Class = _Class; this.TimeToLive = _TimeToLive; } public ResourceRecord(string _Name, Types _Type, Classes _Class, int _TimeToLive, string _RText) { this.Name = _Name; this.Type = _Type; this.Class = _Class; this.TimeToLive = _TimeToLive; this.RText = _RText; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("Name=" + Name + "&Type=" + Type + "&Class=" + Class + "&TTL="+TimeToLive); //TODO: Return TTL as minutes? //TimeSpan timeSpan = new TimeSpan(0, 0, 0, TimeToLive, 0); return sb.ToString(); } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/RR/ResourceRecordCollection.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Collections; using System.Text; namespace OpenDNS { /// /// The Collection Class inherits from ArrayList. It has its own implemenation /// of Sort based on the sortable fields. /// public class ResourceRecordCollection : ArrayList { public enum SortFields { Name, TTL } public void Sort(SortFields sortField, bool isAscending) { switch (sortField) { case SortFields.Name: base.Sort(new NameComparer()); break; case SortFields.TTL: base.Sort(new TTLComparer()); break; } if (!isAscending) base.Reverse(); } private sealed class NameComparer : IComparer { public int Compare(object x, object y) { ResourceRecord first = (ResourceRecord) x; ResourceRecord second = (ResourceRecord) y; return first.Name.CompareTo(second.Name); } } private sealed class TTLComparer : IComparer { public int Compare(object x, object y) { ResourceRecord first = (ResourceRecord) x; ResourceRecord second = (ResourceRecord) y; return first.TimeToLive.CompareTo(second.TimeToLive); } } } } ================================================ FILE: shadowsocks-csharp/3rd/opendns/RR/SOA.cs ================================================ /* * Author: Ruy Delgado * Title: OpenDNS * Description: DNS Client Library * Revision: 1.0 * Last Modified: 2005.01.28 * Created On: 2005.01.28 * * Note: Based on DnsLite by Jaimon Mathew * */ using System; using System.Text; namespace OpenDNS { /// /// Start of Authority Resource Record /// public class SOA : ResourceRecord { public string Server; public string Email; public long Serial; public long Refresh; public long Retry; public long Expire; public long Minimum; public SOA(string _Name, Types _Type, Classes _Class, int _TimeToLive, string _Server, string _Email, long _Serial, long _Refresh, long _Retry, long _Expire, long _Minimum):base(_Name, _Type, _Class, _TimeToLive) { Server = _Server; Email = _Email; Serial = _Serial; Refresh = _Refresh; Retry = _Retry; Expire = _Expire; Minimum = _Minimum; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/BarcodeFormat.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing { /// /// Enumerates barcode formats known to this package. /// /// Sean Owen [System.Flags] public enum BarcodeFormat { /// Aztec 2D barcode format. AZTEC = 1, /// CODABAR 1D format. CODABAR = 2, /// Code 39 1D format. CODE_39 = 4, /// Code 93 1D format. CODE_93 = 8, /// Code 128 1D format. CODE_128 = 16, /// Data Matrix 2D barcode format. DATA_MATRIX = 32, /// EAN-8 1D format. EAN_8 = 64, /// EAN-13 1D format. EAN_13 = 128, /// ITF (Interleaved Two of Five) 1D format. ITF = 256, /// MaxiCode 2D barcode format. MAXICODE = 512, /// PDF417 format. PDF_417 = 1024, /// QR Code 2D barcode format. QR_CODE = 2048, /// RSS 14 RSS_14 = 4096, /// RSS EXPANDED RSS_EXPANDED = 8192, /// UPC-A 1D format. UPC_A = 16384, /// UPC-E 1D format. UPC_E = 32768, /// UPC/EAN extension format. Not a stand-alone format. UPC_EAN_EXTENSION = 65536, /// MSI MSI = 131072, /// Plessey PLESSEY = 262144, /// /// UPC_A | UPC_E | EAN_13 | EAN_8 | CODABAR | CODE_39 | CODE_93 | CODE_128 | ITF | RSS_14 | RSS_EXPANDED /// without MSI (to many false-positives) /// All_1D = UPC_A | UPC_E | EAN_13 | EAN_8 | CODABAR | CODE_39 | CODE_93 | CODE_128 | ITF | RSS_14 | RSS_EXPANDED } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/BaseLuminanceSource.cs ================================================ /* * Copyright 2012 ZXing.Net authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing { /// /// The base class for luminance sources which supports /// cropping and rotating based upon the luminance values. /// public abstract class BaseLuminanceSource : LuminanceSource { // the following channel weights give nearly the same // gray scale picture as the java version with BufferedImage.TYPE_BYTE_GRAY // they are used in sub classes for luminance / gray scale calculation protected const int RChannelWeight = 19562; protected const int GChannelWeight = 38550; protected const int BChannelWeight = 7424; protected const int ChannelWeight = 16; /// /// /// protected byte[] luminances; /// /// Initializes a new instance of the class. /// /// The width. /// The height. protected BaseLuminanceSource(int width, int height) : base(width, height) { luminances = new byte[width * height]; } /// /// Initializes a new instance of the class. /// /// The luminance array. /// The width. /// The height. protected BaseLuminanceSource(byte[] luminanceArray, int width, int height) : base(width, height) { luminances = new byte[width * height]; Buffer.BlockCopy(luminanceArray, 0, luminances, 0, width * height); } /// /// Fetches one row of luminance data from the underlying platform's bitmap. Values range from /// 0 (black) to 255 (white). It is preferable for implementations of this method /// to only fetch this row rather than the whole image, since no 2D Readers may be installed and /// getMatrix() may never be called. /// /// The row to fetch, 0 <= y < Height. /// An optional preallocated array. If null or too small, it will be ignored. /// Always use the returned object, and ignore the .length of the array. /// /// An array containing the luminance data. /// override public byte[] getRow(int y, byte[] row) { int width = Width; if (row == null || row.Length < width) { row = new byte[width]; } for (int i = 0; i < width; i++) row[i] = luminances[y * width + i]; return row; } public override byte[] Matrix { get { return luminances; } } /// /// Returns a new object with rotated image data by 90 degrees counterclockwise. /// Only callable if {@link #isRotateSupported()} is true. /// /// /// A rotated version of this object. /// public override LuminanceSource rotateCounterClockwise() { var rotatedLuminances = new byte[Width * Height]; var newWidth = Height; var newHeight = Width; var localLuminances = Matrix; for (var yold = 0; yold < Height; yold++) { for (var xold = 0; xold < Width; xold++) { var ynew = newHeight - xold - 1; var xnew = yold; rotatedLuminances[ynew * newWidth + xnew] = localLuminances[yold * Width + xold]; } } return CreateLuminanceSource(rotatedLuminances, newWidth, newHeight); } /// /// TODO: not implemented yet /// /// /// A rotated version of this object. /// public override LuminanceSource rotateCounterClockwise45() { // TODO: implement a good 45 degrees rotation without lost of information return base.rotateCounterClockwise45(); } /// /// /// Whether this subclass supports counter-clockwise rotation. public override bool RotateSupported { get { return true; } } /// /// Returns a new object with cropped image data. Implementations may keep a reference to the /// original data rather than a copy. Only callable if CropSupported is true. /// /// The left coordinate, 0 <= left < Width. /// The top coordinate, 0 <= top <= Height. /// The width of the rectangle to crop. /// The height of the rectangle to crop. /// /// A cropped version of this object. /// public override LuminanceSource crop(int left, int top, int width, int height) { if (left + width > Width || top + height > Height) { throw new ArgumentException("Crop rectangle does not fit within image data."); } var croppedLuminances = new byte[width * height]; var oldLuminances = Matrix; var oldWidth = Width; var oldRightBound = left + width; var oldBottomBound = top + height; for (int yold = top, ynew = 0; yold < oldBottomBound; yold++, ynew++) { for (int xold = left, xnew = 0; xold < oldRightBound; xold++, xnew++) { croppedLuminances[ynew * width + xnew] = oldLuminances[yold * oldWidth + xold]; } } return CreateLuminanceSource(croppedLuminances, width, height); } /// /// /// Whether this subclass supports cropping. public override bool CropSupported { get { return true; } } /// /// /// Whether this subclass supports invertion. public override bool InversionSupported { get { return true; } } /// /// Should create a new luminance source with the right class type. /// The method is used in methods crop and rotate. /// /// The new luminances. /// The width. /// The height. /// protected abstract LuminanceSource CreateLuminanceSource(byte[] newLuminances, int width, int height); } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/Binarizer.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using ZXing.Common; namespace ZXing { /// This class hierarchy provides a set of methods to convert luminance data to 1 bit data. /// It allows the algorithm to vary polymorphically, for example allowing a very expensive /// thresholding technique for servers and a fast one for mobile. It also permits the implementation /// to vary, e.g. a JNI version for Android and a Java fallback version for other platforms. /// /// dswitkin@google.com (Daniel Switkin) /// public abstract class Binarizer { private readonly LuminanceSource source; /// /// Initializes a new instance of the class. /// /// The source. protected internal Binarizer(LuminanceSource source) { if (source == null) { throw new ArgumentException("Source must be non-null."); } this.source = source; } /// /// Gets the luminance source object. /// virtual public LuminanceSource LuminanceSource { get { return source; } } /// Converts one row of luminance data to 1 bit data. May actually do the conversion, or return /// cached data. Callers should assume this method is expensive and call it as seldom as possible. /// This method is intended for decoding 1D barcodes and may choose to apply sharpening. /// For callers which only examine one row of pixels at a time, the same BitArray should be reused /// and passed in with each call for performance. However it is legal to keep more than one row /// at a time if needed. /// /// The row to fetch, 0 <= y < bitmap height. /// An optional preallocated array. If null or too small, it will be ignored. /// If used, the Binarizer will call BitArray.clear(). Always use the returned object. /// /// The array of bits for this row (true means black). public abstract BitArray getBlackRow(int y, BitArray row); /// Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive /// and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or /// may not apply sharpening. Therefore, a row from this matrix may not be identical to one /// fetched using getBlackRow(), so don't mix and match between them. /// /// The 2D array of bits for the image (true means black). public abstract BitMatrix BlackMatrix { get; } /// Creates a new object with the same type as this Binarizer implementation, but with pristine /// state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache /// of 1 bit data. See Effective Java for why we can't use Java's clone() method. /// /// The LuminanceSource this Binarizer will operate on. /// A new concrete Binarizer implementation object. public abstract Binarizer createBinarizer(LuminanceSource source); /// /// Gets the width of the luminance source object. /// public int Width { get { return source.Width; } } /// /// Gets the height of the luminance source object. /// public int Height { get { return source.Height; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/BinaryBitmap.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using ZXing.Common; namespace ZXing { /// This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects /// accept a BinaryBitmap and attempt to decode it. /// /// /// dswitkin@google.com (Daniel Switkin) /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class BinaryBitmap { private Binarizer binarizer; private BitMatrix matrix; public BinaryBitmap(Binarizer binarizer) { if (binarizer == null) { throw new ArgumentException("Binarizer must be non-null."); } this.binarizer = binarizer; } /// The width of the bitmap. /// public int Width { get { return binarizer.Width; } } /// The height of the bitmap. /// public int Height { get { return binarizer.Height; } } /// Converts one row of luminance data to 1 bit data. May actually do the conversion, or return /// cached data. Callers should assume this method is expensive and call it as seldom as possible. /// This method is intended for decoding 1D barcodes and may choose to apply sharpening. /// /// /// The row to fetch, 0 <= y < bitmap height. /// /// An optional preallocated array. If null or too small, it will be ignored. /// If used, the Binarizer will call BitArray.clear(). Always use the returned object. /// /// The array of bits for this row (true means black). /// public BitArray getBlackRow(int y, BitArray row) { return binarizer.getBlackRow(y, row); } /// Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive /// and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or /// may not apply sharpening. Therefore, a row from this matrix may not be identical to one /// fetched using getBlackRow(), so don't mix and match between them. /// /// /// The 2D array of bits for the image (true means black). /// public BitMatrix BlackMatrix { get { // The matrix is created on demand the first time it is requested, then cached. There are two // reasons for this: // 1. This work will never be done if the caller only installs 1D Reader objects, or if a // 1D Reader finds a barcode before the 2D Readers run. // 2. This work will only be done once even if the caller installs multiple 2D Readers. if (matrix == null) { matrix = binarizer.BlackMatrix; } return matrix; } } /// Whether this bitmap can be cropped. /// public bool CropSupported { get { return binarizer.LuminanceSource.CropSupported; } } /// Returns a new object with cropped image data. Implementations may keep a reference to the /// original data rather than a copy. Only callable if isCropSupported() is true. /// /// /// The left coordinate, 0 <= left < getWidth(). /// /// The top coordinate, 0 <= top <= getHeight(). /// /// The width of the rectangle to crop. /// /// The height of the rectangle to crop. /// /// A cropped version of this object. /// public BinaryBitmap crop(int left, int top, int width, int height) { var newSource = binarizer.LuminanceSource.crop(left, top, width, height); return new BinaryBitmap(binarizer.createBinarizer(newSource)); } /// Whether this bitmap supports counter-clockwise rotation. /// public bool RotateSupported { get { return binarizer.LuminanceSource.RotateSupported; } } /// /// Returns a new object with rotated image data by 90 degrees counterclockwise. /// Only callable if {@link #isRotateSupported()} is true. /// /// A rotated version of this object. /// public BinaryBitmap rotateCounterClockwise() { var newSource = binarizer.LuminanceSource.rotateCounterClockwise(); return new BinaryBitmap(binarizer.createBinarizer(newSource)); } /// /// Returns a new object with rotated image data by 45 degrees counterclockwise. /// Only callable if {@link #isRotateSupported()} is true. /// /// A rotated version of this object. public BinaryBitmap rotateCounterClockwise45() { LuminanceSource newSource = binarizer.LuminanceSource.rotateCounterClockwise45(); return new BinaryBitmap(binarizer.createBinarizer(newSource)); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { var blackMatrix = BlackMatrix; return blackMatrix != null ? blackMatrix.ToString() : String.Empty; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/BitmapLuminanceSource.cs ================================================ /* * Copyright 2012 ZXing.Net authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Drawing.Imaging; using System.Drawing; using System.Runtime.InteropServices; namespace ZXing { public partial class BitmapLuminanceSource : BaseLuminanceSource { /// /// Initializes a new instance of the class. /// /// The width. /// The height. protected BitmapLuminanceSource(int width, int height) : base(width, height) { } /// /// Initializes a new instance of the class /// with the image of a Bitmap instance /// /// The bitmap. public BitmapLuminanceSource(Bitmap bitmap) : base(bitmap.Width, bitmap.Height) { var height = bitmap.Height; var width = bitmap.Width; // In order to measure pure decoding speed, we convert the entire image to a greyscale array // The underlying raster of image consists of bytes with the luminance values #if WindowsCE var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); #else var data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bitmap.PixelFormat); #endif try { var stride = Math.Abs(data.Stride); var pixelWidth = stride/width; if (pixelWidth > 4) { // old slow way for unsupported bit depth Color c; for (int y = 0; y < height; y++) { int offset = y*width; for (int x = 0; x < width; x++) { c = bitmap.GetPixel(x, y); luminances[offset + x] = (byte)((RChannelWeight * c.R + GChannelWeight * c.G + BChannelWeight * c.B) >> ChannelWeight); } } } else { var strideStep = data.Stride; var buffer = new byte[stride]; var ptrInBitmap = data.Scan0; #if !WindowsCE // prepare palette for 1 and 8 bit indexed bitmaps var luminancePalette = new byte[bitmap.Palette.Entries.Length]; for (var index = 0; index < bitmap.Palette.Entries.Length; index++) { var color = bitmap.Palette.Entries[index]; luminancePalette[index] = (byte) ((RChannelWeight*color.R + GChannelWeight*color.G + BChannelWeight*color.B) >> ChannelWeight); } if (bitmap.PixelFormat == PixelFormat.Format32bppArgb || bitmap.PixelFormat == PixelFormat.Format32bppPArgb) { pixelWidth = 40; } if ((int)bitmap.PixelFormat == 8207 || (bitmap.Flags & (int)ImageFlags.ColorSpaceCmyk) == (int)ImageFlags.ColorSpaceCmyk) { pixelWidth = 41; } #endif for (int y = 0; y < height; y++) { // copy a scanline not the whole bitmap because of memory usage Marshal.Copy(ptrInBitmap, buffer, 0, stride); #if NET40 ptrInBitmap = IntPtr.Add(ptrInBitmap, strideStep); #else ptrInBitmap = new IntPtr(ptrInBitmap.ToInt64() + strideStep); #endif var offset = y*width; switch (pixelWidth) { #if !WindowsCE case 0: for (int x = 0; x*8 < width; x++) { for (int subX = 0; subX < 8 && 8*x + subX < width; subX++) { var index = (buffer[x] >> (7 - subX)) & 1; luminances[offset + 8*x + subX] = luminancePalette[index]; } } break; case 1: for (int x = 0; x < width; x++) { luminances[offset + x] = luminancePalette[buffer[x]]; } break; #endif case 2: // should be RGB565 or RGB555, assume RGB565 { var maxIndex = 2*width; for (int index = 0; index < maxIndex; index += 2) { var byte1 = buffer[index]; var byte2 = buffer[index + 1]; var b5 = byte1 & 0x1F; var g5 = (((byte1 & 0xE0) >> 5) | ((byte2 & 0x03) << 3)) & 0x1F; var r5 = (byte2 >> 2) & 0x1F; var r8 = (r5*527 + 23) >> 6; var g8 = (g5*527 + 23) >> 6; var b8 = (b5*527 + 23) >> 6; luminances[offset] = (byte)((RChannelWeight * r8 + GChannelWeight * g8 + BChannelWeight * b8) >> ChannelWeight); offset++; } } break; case 3: { var maxIndex = width*3; for (int x = 0; x < maxIndex; x += 3) { var luminance = (byte) ((BChannelWeight*buffer[x] + GChannelWeight*buffer[x + 1] + RChannelWeight*buffer[x + 2]) >> ChannelWeight); luminances[offset] = luminance; offset++; } } break; case 4: // 4 bytes without alpha channel value { var maxIndex = 4*width; for (int x = 0; x < maxIndex; x += 4) { var luminance = (byte) ((BChannelWeight*buffer[x] + GChannelWeight*buffer[x + 1] + RChannelWeight*buffer[x + 2]) >> ChannelWeight); luminances[offset] = luminance; offset++; } } break; case 40: // with alpha channel; some barcodes are completely black if you // only look at the r, g and b channel but the alpha channel controls // the view { var maxIndex = 4*width; for (int x = 0; x < maxIndex; x += 4) { var luminance = (byte) ((BChannelWeight*buffer[x] + GChannelWeight*buffer[x + 1] + RChannelWeight*buffer[x + 2]) >> ChannelWeight); // calculating the resulting luminance based upon a white background // var alpha = buffer[x * pixelWidth + 3] / 255.0; // luminance = (byte)(luminance * alpha + 255 * (1 - alpha)); var alpha = buffer[x + 3]; luminance = (byte) (((luminance*alpha) >> 8) + (255*(255 - alpha) >> 8) + 1); luminances[offset] = luminance; offset++; } } break; case 41: // CMYK color space { var maxIndex = 4 * width; for (int x = 0; x < maxIndex; x += 4) { var luminance = (byte) (255 - ((BChannelWeight*buffer[x] + GChannelWeight*buffer[x + 1] + RChannelWeight*buffer[x + 2]) >> ChannelWeight)); // Ignore value of k at the moment luminances[offset] = luminance; offset++; } } break; default: throw new NotSupportedException(); } } } } finally { bitmap.UnlockBits(data); } } /// /// Should create a new luminance source with the right class type. /// The method is used in methods crop and rotate. /// /// The new luminances. /// The width. /// The height. /// protected override LuminanceSource CreateLuminanceSource(byte[] newLuminances, int width, int height) { return new BitmapLuminanceSource(width, height) { luminances = newLuminances }; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/DecodeHintType.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; namespace ZXing { /// /// Encapsulates a type of hint that a caller may pass to a barcode reader to help it /// more quickly or accurately decode it. It is up to implementations to decide what, /// if anything, to do with the information that is supplied. /// /// /// Sean Owen /// dswitkin@google.com (Daniel Switkin) public enum DecodeHintType { /// /// Unspecified, application-specific hint. Maps to an unspecified . /// OTHER, /// /// Image is a pure monochrome image of a barcode. Doesn't matter what it maps to; /// use = true. /// PURE_BARCODE, /// /// Image is known to be of one of a few possible formats. /// Maps to a of s. /// POSSIBLE_FORMATS, /// /// Spend more time to try to find a barcode; optimize for accuracy, not speed. /// Doesn't matter what it maps to; use = true. /// TRY_HARDER, /// /// Specifies what character encoding to use when decoding, where applicable (type String) /// CHARACTER_SET, /// /// Allowed lengths of encoded data -- reject anything else. Maps to an int[]. /// ALLOWED_LENGTHS, /// /// Assume Code 39 codes employ a check digit. Maps to . /// ASSUME_CODE_39_CHECK_DIGIT, /// /// The caller needs to be notified via callback when a possible /// is found. Maps to a . /// NEED_RESULT_POINT_CALLBACK, /// /// Assume MSI codes employ a check digit. Maps to . /// ASSUME_MSI_CHECK_DIGIT, /// /// if Code39 could be detected try to use extended mode for full ASCII character set /// Maps to . /// USE_CODE_39_EXTENDED_MODE, /// /// Don't fail if a Code39 is detected but can't be decoded in extended mode. /// Return the raw Code39 result instead. Maps to . /// RELAXED_CODE_39_EXTENDED_MODE, /// /// 1D readers supporting rotation with TRY_HARDER enabled. /// But BarcodeReader class can do auto-rotating for 1D and 2D codes. /// Enabling that option prevents 1D readers doing double rotation. /// BarcodeReader enables that option automatically if "global" auto-rotation is enabled. /// Maps to . /// TRY_HARDER_WITHOUT_ROTATION, /// /// Assume the barcode is being processed as a GS1 barcode, and modify behavior as needed. /// For example this affects FNC1 handling for Code 128 (aka GS1-128). Doesn't matter what it maps to; /// use . /// ASSUME_GS1, /// /// If true, return the start and end digits in a Codabar barcode instead of stripping them. They /// are alpha, whereas the rest are numeric. By default, they are stripped, but this causes them /// to not be. Doesn't matter what it maps to; use . /// RETURN_CODABAR_START_END, /// /// Allowed extension lengths for EAN or UPC barcodes. Other formats will ignore this. /// Maps to an of the allowed extension lengths, for example [2], [5], or [2, 5]. /// If it is optional to have an extension, do not set this hint. If this is set, /// and a UPC or EAN barcode is found but an extension is not, then no result will be returned /// at all. /// ALLOWED_EAN_EXTENSIONS } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/EncodeHintType.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing { /// /// These are a set of hints that you may pass to Writers to specify their behavior. /// /// dswitkin@google.com (Daniel Switkin) public enum EncodeHintType { /// /// Specifies the width of the barcode image /// type: /// WIDTH, /// /// Specifies the height of the barcode image /// type: /// HEIGHT, /// /// Don't put the content string into the output image. /// type: /// PURE_BARCODE, /// /// Specifies what degree of error correction to use, for example in QR Codes. /// Type depends on the encoder. For example for QR codes it's type /// /// For Aztec it is of type , representing the minimal percentage of error correction words. /// Note: an Aztec symbol should have a minimum of 25% EC words. /// For PDF417 it is of type or (between 0 and 8), /// ERROR_CORRECTION, /// /// Specifies what character encoding to use where applicable. /// type: /// CHARACTER_SET, /// /// Specifies margin, in pixels, to use when generating the barcode. The meaning can vary /// by format; for example it controls margin before and after the barcode horizontally for /// most 1D formats. /// type: /// MARGIN, /// /// Specifies whether to use compact mode for PDF417. /// type: /// PDF417_COMPACT, /// /// Specifies what compaction mode to use for PDF417. /// type: /// PDF417_COMPACTION, /// /// Specifies the minimum and maximum number of rows and columns for PDF417. /// type: /// PDF417_DIMENSIONS, /// /// Don't append ECI segment. /// That is against the specification of QR Code but some /// readers have problems if the charset is switched from /// ISO-8859-1 (default) to UTF-8 with the necessary ECI segment. /// If you set the property to true you can use UTF-8 encoding /// and the ECI segment is omitted. /// type: /// DISABLE_ECI, /// /// Specifies the matrix shape for Data Matrix (type ) /// DATA_MATRIX_SHAPE, /// /// Specifies a minimum barcode size (type ). Only applicable to Data Matrix now. /// MIN_SIZE, /// /// Specifies a maximum barcode size (type ). Only applicable to Data Matrix now. /// MAX_SIZE, /// /// if true, don't switch to codeset C for numbers /// CODE128_FORCE_CODESET_B, /// /// Specifies the default encodation for Data Matrix (type ) /// Make sure that the content fits into the encodation value, otherwise there will be an exception thrown. /// standard value: Encodation.ASCII /// DATA_MATRIX_DEFAULT_ENCODATION, /// /// Specifies the required number of layers for an Aztec code: /// a negative number (-1, -2, -3, -4) specifies a compact Aztec code /// 0 indicates to use the minimum number of layers (the default) /// a positive number (1, 2, .. 32) specifies a normal (non-compact) Aztec code /// AZTEC_LAYERS, } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/LuminanceSource.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Text; namespace ZXing { /// The purpose of this class hierarchy is to abstract different bitmap implementations across /// platforms into a standard interface for requesting greyscale luminance values. The interface /// only provides immutable methods; therefore crop and rotation create copies. This is to ensure /// that one Reader does not modify the original luminance source and leave it in an unknown state /// for other Readers in the chain. /// /// dswitkin@google.com (Daniel Switkin) /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public abstract class LuminanceSource { private int width; private int height; protected LuminanceSource(int width, int height) { this.width = width; this.height = height; } /// Fetches one row of luminance data from the underlying platform's bitmap. Values range from /// 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have /// to bitwise and with 0xff for each value. It is preferable for implementations of this method /// to only fetch this row rather than the whole image, since no 2D Readers may be installed and /// getMatrix() may never be called. /// /// /// The row to fetch, 0 <= y < Height. /// /// An optional preallocated array. If null or too small, it will be ignored. /// Always use the returned object, and ignore the .length of the array. /// /// An array containing the luminance data. /// public abstract byte[] getRow(int y, byte[] row); /// Fetches luminance data for the underlying bitmap. Values should be fetched using: /// int luminance = array[y * width + x] & 0xff; /// /// /// A row-major 2D array of luminance values. Do not use result.length as it may be /// larger than width * height bytes on some platforms. Do not modify the contents /// of the result. /// public abstract byte[] Matrix { get; } /// The width of the bitmap. virtual public int Width { get { return width; } protected set { width = value; } } /// The height of the bitmap. virtual public int Height { get { return height; } protected set { height = value; } } /// Whether this subclass supports cropping. virtual public bool CropSupported { get { return false; } } /// Returns a new object with cropped image data. Implementations may keep a reference to the /// original data rather than a copy. Only callable if CropSupported is true. /// /// /// The left coordinate, 0 <= left < Width. /// /// The top coordinate, 0 <= top <= Height. /// /// The width of the rectangle to crop. /// /// The height of the rectangle to crop. /// /// A cropped version of this object. /// public virtual LuminanceSource crop(int left, int top, int width, int height) { throw new NotSupportedException("This luminance source does not support cropping."); } /// Whether this subclass supports counter-clockwise rotation. virtual public bool RotateSupported { get { return false; } } /// /// Returns a new object with rotated image data by 90 degrees counterclockwise. /// Only callable if {@link #isRotateSupported()} is true. /// /// A rotated version of this object. /// public virtual LuminanceSource rotateCounterClockwise() { throw new NotSupportedException("This luminance source does not support rotation."); } /// /// Returns a new object with rotated image data by 45 degrees counterclockwise. /// Only callable if {@link #isRotateSupported()} is true. /// /// A rotated version of this object. public virtual LuminanceSource rotateCounterClockwise45() { throw new NotSupportedException("This luminance source does not support rotation by 45 degrees."); } /// /// /// Whether this subclass supports invertion. virtual public bool InversionSupported { get { return false; } } override public String ToString() { var row = new byte[width]; var result = new StringBuilder(height * (width + 1)); for (int y = 0; y < height; y++) { row = getRow(y, row); for (int x = 0; x < width; x++) { int luminance = row[x] & 0xFF; char c; if (luminance < 0x40) { c = '#'; } else if (luminance < 0x80) { c = '+'; } else if (luminance < 0xC0) { c = '.'; } else { c = ' '; } result.Append(c); } result.Append('\n'); } return result.ToString(); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/Result.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; namespace ZXing { /// /// Encapsulates the result of decoding a barcode within an image. /// public sealed class Result { /// raw text encoded by the barcode, if applicable, otherwise null public String Text { get; private set; } /// raw bytes encoded by the barcode, if applicable, otherwise null public byte[] RawBytes { get; private set; } /// /// points related to the barcode in the image. These are typically points /// identifying finder patterns or the corners of the barcode. The exact meaning is /// specific to the type of barcode that was decoded. /// public ResultPoint[] ResultPoints { get; private set; } /// {@link BarcodeFormat} representing the format of the barcode that was decoded public BarcodeFormat BarcodeFormat { get; private set; } /// /// {@link Hashtable} mapping {@link ResultMetadataType} keys to values. May be /// null. This contains optional metadata about what was detected about the barcode, /// like orientation. /// public IDictionary ResultMetadata { get; private set; } /// /// Gets the timestamp. /// public long Timestamp { get; private set; } /// /// Initializes a new instance of the class. /// /// The text. /// The raw bytes. /// The result points. /// The format. public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format) : this(text, rawBytes, resultPoints, format, DateTime.Now.Ticks) { } /// /// Initializes a new instance of the class. /// /// The text. /// The raw bytes. /// The result points. /// The format. /// The timestamp. public Result(String text, byte[] rawBytes, ResultPoint[] resultPoints, BarcodeFormat format, long timestamp) { if (text == null && rawBytes == null) { throw new ArgumentException("Text and bytes are null"); } Text = text; RawBytes = rawBytes; ResultPoints = resultPoints; BarcodeFormat = format; ResultMetadata = null; Timestamp = timestamp; } /// /// Adds one metadata to the result /// /// The type. /// The value. public void putMetadata(ResultMetadataType type, Object value) { if (ResultMetadata == null) { ResultMetadata = new Dictionary(); } ResultMetadata[type] = value; } /// /// Adds a list of metadata to the result /// /// The metadata. public void putAllMetadata(IDictionary metadata) { if (metadata != null) { if (ResultMetadata == null) { ResultMetadata = metadata; } else { foreach (var entry in metadata) ResultMetadata[entry.Key] = entry.Value; } } } /// /// Adds the result points. /// /// The new points. public void addResultPoints(ResultPoint[] newPoints) { var oldPoints = ResultPoints; if (oldPoints == null) { ResultPoints = newPoints; } else if (newPoints != null && newPoints.Length > 0) { var allPoints = new ResultPoint[oldPoints.Length + newPoints.Length]; Array.Copy(oldPoints, 0, allPoints, 0, oldPoints.Length); Array.Copy(newPoints, 0, allPoints, oldPoints.Length, newPoints.Length); ResultPoints = allPoints; } } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { if (Text == null) { return "[" + RawBytes.Length + " bytes]"; } return Text; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/ResultMetadataType.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing { /// /// Represents some type of metadata about the result of the decoding that the decoder /// wishes to communicate back to the caller. /// /// Sean Owen public enum ResultMetadataType { /// /// Unspecified, application-specific metadata. Maps to an unspecified {@link Object}. /// OTHER, /// /// Denotes the likely approximate orientation of the barcode in the image. This value /// is given as degrees rotated clockwise from the normal, upright orientation. /// For example a 1D barcode which was found by reading top-to-bottom would be /// said to have orientation "90". This key maps to an {@link Integer} whose /// value is in the range [0,360). /// ORIENTATION, /// ///

2D barcode formats typically encode text, but allow for a sort of 'byte mode' /// which is sometimes used to encode binary data. While {@link Result} makes available /// the complete raw bytes in the barcode for these formats, it does not offer the bytes /// from the byte segments alone.

///

This maps to a {@link java.util.List} of byte arrays corresponding to the /// raw bytes in the byte segments in the barcode, in order.

///
BYTE_SEGMENTS, /// /// Error correction level used, if applicable. The value type depends on the /// format, but is typically a String. /// ERROR_CORRECTION_LEVEL, /// /// For some periodicals, indicates the issue number as an {@link Integer}. /// ISSUE_NUMBER, /// /// For some products, indicates the suggested retail price in the barcode as a /// formatted {@link String}. /// SUGGESTED_PRICE, /// /// For some products, the possible country of manufacture as a {@link String} denoting the /// ISO country code. Some map to multiple possible countries, like "US/CA". /// POSSIBLE_COUNTRY, /// /// For some products, the extension text /// UPC_EAN_EXTENSION, /// /// If the code format supports structured append and /// the current scanned code is part of one then the /// sequence number is given with it. /// STRUCTURED_APPEND_SEQUENCE, /// /// If the code format supports structured append and /// the current scanned code is part of one then the /// parity is given with it. /// STRUCTURED_APPEND_PARITY, /// /// PDF417-specific metadata /// PDF417_EXTRA_METADATA, /// /// Aztec-specific metadata /// AZTEC_EXTRA_METADATA } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/ResultPoint.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using ZXing.Common.Detector; namespace ZXing { /// /// Encapsulates a point of interest in an image containing a barcode. Typically, this /// would be the location of a finder pattern or the corner of the barcode, for example. /// /// Sean Owen /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source public class ResultPoint { private readonly float x; private readonly float y; private readonly byte[] bytesX; private readonly byte[] bytesY; private String toString; /// /// Initializes a new instance of the class. /// public ResultPoint() { } /// /// Initializes a new instance of the class. /// /// The x. /// The y. public ResultPoint(float x, float y) { this.x = x; this.y = y; // calculate only once for GetHashCode bytesX = BitConverter.GetBytes(x); bytesY = BitConverter.GetBytes(y); } /// /// Gets the X. /// virtual public float X { get { return x; } } /// /// Gets the Y. /// virtual public float Y { get { return y; } } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(Object other) { var otherPoint = other as ResultPoint; if (otherPoint == null) return false; return x == otherPoint.x && y == otherPoint.y; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { return 31 * ((bytesX[0] << 24) + (bytesX[1] << 16) + (bytesX[2] << 8) + bytesX[3]) + (bytesY[0] << 24) + (bytesY[1] << 16) + (bytesY[2] << 8) + bytesY[3]; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { if (toString == null) { var result = new System.Text.StringBuilder(25); result.AppendFormat(System.Globalization.CultureInfo.CurrentUICulture, "({0}, {1})", x, y); toString = result.ToString(); } return toString; } /// /// Orders an array of three ResultPoints in an order [A,B,C] such that AB < AC and /// BC < AC and the angle between BC and BA is less than 180 degrees. /// public static void orderBestPatterns(ResultPoint[] patterns) { // Find distances between pattern centers float zeroOneDistance = distance(patterns[0], patterns[1]); float oneTwoDistance = distance(patterns[1], patterns[2]); float zeroTwoDistance = distance(patterns[0], patterns[2]); ResultPoint pointA, pointB, pointC; // Assume one closest to other two is B; A and C will just be guesses at first if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) { pointB = patterns[0]; pointA = patterns[1]; pointC = patterns[2]; } else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) { pointB = patterns[1]; pointA = patterns[0]; pointC = patterns[2]; } else { pointB = patterns[2]; pointA = patterns[0]; pointC = patterns[1]; } // Use cross product to figure out whether A and C are correct or flipped. // This asks whether BC x BA has a positive z component, which is the arrangement // we want for A, B, C. If it's negative, then we've got it flipped around and // should swap A and C. if (crossProductZ(pointA, pointB, pointC) < 0.0f) { ResultPoint temp = pointA; pointA = pointC; pointC = temp; } patterns[0] = pointA; patterns[1] = pointB; patterns[2] = pointC; } /// /// distance between two points /// public static float distance(ResultPoint pattern1, ResultPoint pattern2) { return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y); } /// /// Returns the z component of the cross product between vectors BC and BA. /// private static float crossProductZ(ResultPoint pointA, ResultPoint pointB, ResultPoint pointC) { float bX = pointB.x; float bY = pointB.y; return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX)); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/ResultPointCallback.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing { /// Callback which is invoked when a possible result point (significant /// point in the barcode image such as a corner) is found. /// /// /// /// public delegate void ResultPointCallback(ResultPoint point); } ================================================ FILE: shadowsocks-csharp/3rd/zxing/WriterException.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing { /// /// A base class which covers the range of exceptions which may occur when encoding a barcode using /// the Writer framework. /// /// dswitkin@google.com (Daniel Switkin) [Serializable] public sealed class WriterException : Exception { /// /// Initializes a new instance of the class. /// public WriterException() { } /// /// Initializes a new instance of the class. /// /// The message. public WriterException(String message) :base(message) { } /// /// Initializes a new instance of the class. /// /// The message. /// The inner exc. public WriterException(String message, Exception innerExc) : base(message, innerExc) { } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/BitArray.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common { /// /// A simple, fast array of bits, represented compactly by an array of ints internally. /// /// Sean Owen public sealed class BitArray { private int[] bits; private int size; public int Size { get { return size; } } public int SizeInBytes { get { return (size + 7) >> 3; } } public bool this[int i] { get { return (bits[i >> 5] & (1 << (i & 0x1F))) != 0; } set { if (value) bits[i >> 5] |= 1 << (i & 0x1F); } } public BitArray() { this.size = 0; this.bits = new int[1]; } public BitArray(int size) { if (size < 1) { throw new ArgumentException("size must be at least 1"); } this.size = size; this.bits = makeArray(size); } // For testing only private BitArray(int[] bits, int size) { this.bits = bits; this.size = size; } private void ensureCapacity(int size) { if (size > bits.Length << 5) { int[] newBits = makeArray(size); System.Array.Copy(bits, 0, newBits, 0, bits.Length); bits = newBits; } } private static int numberOfTrailingZeros(int num) { var index = (-num & num)%37; if (index < 0) index *= -1; return _lookup[index]; } private static readonly int[] _lookup = { 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 }; /// Sets a block of 32 bits, starting at bit i. /// /// /// first bit to set /// /// the new value of the next 32 bits. Note again that the least-significant bit /// corresponds to bit i, the next-least-significant to i+1, and so on. /// public void setBulk(int i, int newBits) { bits[i >> 5] = newBits; } /// Clears all bits (sets to false). public void clear() { int max = bits.Length; for (int i = 0; i < max; i++) { bits[i] = 0; } } /// /// Appends the bit. /// /// The bit. public void appendBit(bool bit) { ensureCapacity(size + 1); if (bit) { bits[size >> 5] |= 1 << (size & 0x1F); } size++; } /// underlying array of ints. The first element holds the first 32 bits, and the least /// significant bit is bit 0. /// public int[] Array { get { return bits; } } /// /// Appends the least-significant bits, from value, in order from most-significant to /// least-significant. For example, appending 6 bits from 0x000001E will append the bits /// 0, 1, 1, 1, 1, 0 in that order. /// /// The value. /// The num bits. public void appendBits(int value, int numBits) { if (numBits < 0 || numBits > 32) { throw new ArgumentException("Num bits must be between 0 and 32"); } ensureCapacity(size + numBits); for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) { appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1); } } public void appendBitArray(BitArray other) { int otherSize = other.size; ensureCapacity(size + otherSize); for (int i = 0; i < otherSize; i++) { appendBit(other[i]); } } public void xor(BitArray other) { if (bits.Length != other.bits.Length) { throw new ArgumentException("Sizes don't match"); } for (int i = 0; i < bits.Length; i++) { // The last byte could be incomplete (i.e. not have 8 bits in // it) but there is no problem since 0 XOR 0 == 0. bits[i] ^= other.bits[i]; } } /// /// Toes the bytes. /// /// first bit to start writing /// array to write into. Bytes are written most-significant byte first. This is the opposite /// of the internal representation, which is exposed by BitArray /// position in array to start writing /// how many bytes to write public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) { for (int i = 0; i < numBytes; i++) { int theByte = 0; for (int j = 0; j < 8; j++) { if (this[bitOffset]) { theByte |= 1 << (7 - j); } bitOffset++; } array[offset + i] = (byte)theByte; } } private static int[] makeArray(int size) { return new int[(size + 31) >> 5]; } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(Object o) { var other = o as BitArray; if (other == null) return false; if (size != other.size) return false; for (var index = 0; index < size; index++) { if (bits[index] != other.bits[index]) return false; } return true; } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { var hash = size; foreach (var bit in bits) { hash = 31 * hash + bit.GetHashCode(); } return hash; } /// /// Erstellt ein neues Objekt, das eine Kopie der aktuellen Instanz darstellt. /// /// /// Ein neues Objekt, das eine Kopie dieser Instanz darstellt. /// public object Clone() { return new BitArray((int[])bits.Clone(), size); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/BitMatrix.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common { /// ///

Represents a 2D matrix of bits. In function arguments below, and throughout the common /// module, x is the column position, and y is the row position. The ordering is always x, y. /// The origin is at the top-left.

///

Internally the bits are represented in a 1-D array of 32-bit ints. However, each row begins /// with a new int. This is done intentionally so that we can copy out a row into a BitArray very /// efficiently.

///

The ordering of bits is row-major. Within each int, the least significant bits are used first, /// meaning they represent lower x values. This is compatible with BitArray's implementation.

///
/// Sean Owen /// dswitkin@google.com (Daniel Switkin) public sealed partial class BitMatrix { private readonly int width; private readonly int height; private readonly int rowSize; private readonly int[] bits; /// The width of the matrix /// public int Width { get { return width; } } /// The height of the matrix /// public int Height { get { return height; } } // A helper to construct a square matrix. public BitMatrix(int dimension) : this(dimension, dimension) { } public BitMatrix(int width, int height) { if (width < 1 || height < 1) { throw new System.ArgumentException("Both dimensions must be greater than 0"); } this.width = width; this.height = height; this.rowSize = (width + 31) >> 5; bits = new int[rowSize * height]; } private BitMatrix(int width, int height, int rowSize, int[] bits) { this.width = width; this.height = height; this.rowSize = rowSize; this.bits = bits; } ///

Gets the requested bit, where true means black.

/// ///
/// The horizontal component (i.e. which column) /// /// The vertical component (i.e. which row) /// /// value of given bit in matrix /// public bool this[int x, int y] { get { int offset = y * rowSize + (x >> 5); return (((int)((uint)(bits[offset]) >> (x & 0x1f))) & 1) != 0; } set { if (value) { int offset = y * rowSize + (x >> 5); bits[offset] |= 1 << (x & 0x1f); } } } ///

Flips the given bit.

/// ///
/// The horizontal component (i.e. which column) /// /// The vertical component (i.e. which row) /// public void flip(int x, int y) { int offset = y * rowSize + (x >> 5); bits[offset] ^= 1 << (x & 0x1f); } ///

Sets a square region of the bit matrix to true.

/// ///
/// The horizontal position to begin at (inclusive) /// /// The vertical position to begin at (inclusive) /// /// The width of the region /// /// The height of the region /// public void setRegion(int left, int top, int width, int height) { if (top < 0 || left < 0) { throw new System.ArgumentException("Left and top must be nonnegative"); } if (height < 1 || width < 1) { throw new System.ArgumentException("Height and width must be at least 1"); } int right = left + width; int bottom = top + height; if (bottom > this.height || right > this.width) { throw new System.ArgumentException("The region must fit inside the matrix"); } for (int y = top; y < bottom; y++) { int offset = y * rowSize; for (int x = left; x < right; x++) { bits[offset + (x >> 5)] |= 1 << (x & 0x1f); } } } /// A fast method to retrieve one row of data from the matrix as a BitArray. /// /// /// The row to retrieve /// /// An optional caller-allocated BitArray, will be allocated if null or too small /// /// The resulting BitArray - this reference should always be used even when passing /// your own row /// public BitArray getRow(int y, BitArray row) { if (row == null || row.Size < width) { row = new BitArray(width); } else { row.clear(); } int offset = y * rowSize; for (int x = 0; x < rowSize; x++) { row.setBulk(x << 5, bits[offset + x]); } return row; } /// /// Sets the row. /// /// row to set /// {@link BitArray} to copy from public void setRow(int y, BitArray row) { Array.Copy(row.Array, 0, bits, y * rowSize, rowSize); } /// /// This is useful in detecting a corner of a 'pure' barcode. /// /// {x,y} coordinate of top-left-most 1 bit, or null if it is all white public int[] getTopLeftOnBit() { int bitsOffset = 0; while (bitsOffset < bits.Length && bits[bitsOffset] == 0) { bitsOffset++; } if (bitsOffset == bits.Length) { return null; } int y = bitsOffset / rowSize; int x = (bitsOffset % rowSize) << 5; int theBits = bits[bitsOffset]; int bit = 0; while ((theBits << (31 - bit)) == 0) { bit++; } x += bit; return new[] { x, y }; } public int[] getBottomRightOnBit() { int bitsOffset = bits.Length - 1; while (bitsOffset >= 0 && bits[bitsOffset] == 0) { bitsOffset--; } if (bitsOffset < 0) { return null; } int y = bitsOffset / rowSize; int x = (bitsOffset % rowSize) << 5; int theBits = bits[bitsOffset]; int bit = 31; while (((int)((uint)theBits >> bit)) == 0) // (theBits >>> bit) { bit--; } x += bit; return new int[] { x, y }; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/BitSource.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common { ///

This provides an easy abstraction to read bits at a time from a sequence of bytes, where the /// number of bits read is not often a multiple of 8.

/// ///

This class is thread-safe but not reentrant. Unless the caller modifies the bytes array /// it passed in, in which case all bets are off.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class BitSource { private readonly byte[] bytes; private int byteOffset; private int bitOffset; /// bytes from which this will read bits. Bits will be read from the first byte first. /// Bits are read within a byte from most-significant to least-significant bit. /// public BitSource(byte[] bytes) { this.bytes = bytes; } /// /// index of next bit in current byte which would be read by the next call to {@link #readBits(int)}. /// public int BitOffset { get { return bitOffset; } } /// /// index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}. /// public int ByteOffset { get { return byteOffset; } } /// number of bits to read /// /// int representing the bits read. The bits will appear as the least-significant /// bits of the int /// /// if numBits isn't in [1,32] or more than is available public int readBits(int numBits) { if (numBits < 1 || numBits > 32 || numBits > available()) { throw new ArgumentException(numBits.ToString(), "numBits"); } int result = 0; // First, read remainder from current byte if (bitOffset > 0) { int bitsLeft = 8 - bitOffset; int toRead = numBits < bitsLeft ? numBits : bitsLeft; int bitsToNotRead = bitsLeft - toRead; int mask = (0xFF >> (8 - toRead)) << bitsToNotRead; result = (bytes[byteOffset] & mask) >> bitsToNotRead; numBits -= toRead; bitOffset += toRead; if (bitOffset == 8) { bitOffset = 0; byteOffset++; } } // Next read whole bytes if (numBits > 0) { while (numBits >= 8) { result = (result << 8) | (bytes[byteOffset] & 0xFF); byteOffset++; numBits -= 8; } // Finally read a partial byte if (numBits > 0) { int bitsToNotRead = 8 - numBits; int mask = (0xFF >> bitsToNotRead) << bitsToNotRead; result = (result << numBits) | ((bytes[byteOffset] & mask) >> bitsToNotRead); bitOffset += numBits; } } return result; } /// number of bits that can be read successfully /// public int available() { return 8 * (bytes.Length - byteOffset) - bitOffset; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/DecoderResult.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; namespace ZXing.Common { /// /// Encapsulates the result of decoding a matrix of bits. This typically /// applies to 2D barcode formats. For now it contains the raw bytes obtained, /// as well as a String interpretation of those bytes, if applicable. /// Sean Owen /// public sealed class DecoderResult { public byte[] RawBytes { get; private set; } public String Text { get; private set; } public IList ByteSegments { get; private set; } public String ECLevel { get; private set; } public bool StructuredAppend { get { return StructuredAppendParity >= 0 && StructuredAppendSequenceNumber >= 0; } } public int ErrorsCorrected { get; set; } public int StructuredAppendSequenceNumber { get; private set; } public int Erasures { get; set; } public int StructuredAppendParity { get; private set; } /// /// Miscellanseous data value for the various decoders /// /// The other. public object Other { get; set; } public DecoderResult(byte[] rawBytes, String text, IList byteSegments, String ecLevel) : this(rawBytes, text, byteSegments, ecLevel, -1, -1) { } public DecoderResult(byte[] rawBytes, String text, IList byteSegments, String ecLevel, int saSequence, int saParity) { if (rawBytes == null && text == null) { throw new ArgumentException(); } RawBytes = rawBytes; Text = text; ByteSegments = byteSegments; ECLevel = ecLevel; StructuredAppendParity = saParity; StructuredAppendSequenceNumber = saSequence; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/DefaultGridSampler.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common { /// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class DefaultGridSampler : GridSampler { public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY) { PerspectiveTransform transform = PerspectiveTransform.quadrilateralToQuadrilateral( p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY); return sampleGrid(image, dimensionX, dimensionY, transform); } public override BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) { if (dimensionX <= 0 || dimensionY <= 0) { return null; } BitMatrix bits = new BitMatrix(dimensionX, dimensionY); float[] points = new float[dimensionX << 1]; for (int y = 0; y < dimensionY; y++) { int max = points.Length; //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" float iValue = (float)y + 0.5f; for (int x = 0; x < max; x += 2) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" points[x] = (float)(x >> 1) + 0.5f; points[x + 1] = iValue; } transform.transformPoints(points); // Quick check to see if points transformed to something inside the image; // sufficient to check the endpoints if (!checkAndNudgePoints(image, points)) return null; try { for (int x = 0; x < max; x += 2) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" bits[x >> 1, y] = image[(int)points[x], (int)points[x + 1]]; } } catch (System.IndexOutOfRangeException) { // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting // transform gets "twisted" such that it maps a straight line of points to a set of points // whose endpoints are in bounds, but others are not. There is probably some mathematical // way to detect this about the transformation that I don't know yet. // This results in an ugly runtime exception despite our clever checks above -- can't have // that. We could check each point's coordinates but that feels duplicative. We settle for // catching and wrapping ArrayIndexOutOfBoundsException. return null; } } return bits; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/DetectorResult.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common { ///

Encapsulates the result of detecting a barcode in an image. This includes the raw /// matrix of black/white pixels corresponding to the barcode, and possibly points of interest /// in the image, like the location of finder patterns or corners of the barcode in the image.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public class DetectorResult { public BitMatrix Bits { get; private set; } public ResultPoint[] Points { get; private set; } public DetectorResult(BitMatrix bits, ResultPoint[] points) { Bits = bits; Points = points; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/GlobalHistogramBinarizer.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common { /// This Binarizer implementation uses the old ZXing global histogram approach. It is suitable /// for low-end mobile devices which don't have enough CPU or memory to use a local thresholding /// algorithm. However, because it picks a global black point, it cannot handle difficult shadows /// and gradients. /// /// Faster mobile devices and all desktop applications should probably use HybridBinarizer instead. /// /// dswitkin@google.com (Daniel Switkin) /// Sean Owen /// public class GlobalHistogramBinarizer : Binarizer { private const int LUMINANCE_BITS = 5; private const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; private const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; private static readonly byte[] EMPTY = new byte[0]; private byte[] luminances; private readonly int[] buckets; /// /// Initializes a new instance of the class. /// /// The source. public GlobalHistogramBinarizer(LuminanceSource source) : base(source) { luminances = EMPTY; buckets = new int[LUMINANCE_BUCKETS]; } /// /// Applies simple sharpening to the row data to improve performance of the 1D Readers. /// /// /// /// public override BitArray getBlackRow(int y, BitArray row) { LuminanceSource source = LuminanceSource; int width = source.Width; if (row == null || row.Size < width) { row = new BitArray(width); } else { row.clear(); } initArrays(width); byte[] localLuminances = source.getRow(y, luminances); int[] localBuckets = buckets; for (int x = 0; x < width; x++) { int pixel = localLuminances[x] & 0xff; localBuckets[pixel >> LUMINANCE_SHIFT]++; } int blackPoint; if (!estimateBlackPoint(localBuckets, out blackPoint)) return null; int left = localLuminances[0] & 0xff; int center = localLuminances[1] & 0xff; for (int x = 1; x < width - 1; x++) { int right = localLuminances[x + 1] & 0xff; // A simple -1 4 -1 box filter with a weight of 2. int luminance = ((center << 2) - left - right) >> 1; row[x] = (luminance < blackPoint); left = center; center = right; } return row; } /// /// Does not sharpen the data, as this call is intended to only be used by 2D Readers. /// override public BitMatrix BlackMatrix { get { LuminanceSource source = LuminanceSource; byte[] localLuminances; int width = source.Width; int height = source.Height; BitMatrix matrix = new BitMatrix(width, height); // Quickly calculates the histogram by sampling four rows from the image. This proved to be // more robust on the blackbox tests than sampling a diagonal as we used to do. initArrays(width); int[] localBuckets = buckets; for (int y = 1; y < 5; y++) { int row = height * y / 5; localLuminances = source.getRow(row, luminances); int right = (width << 2) / 5; for (int x = width / 5; x < right; x++) { int pixel = localLuminances[x] & 0xff; localBuckets[pixel >> LUMINANCE_SHIFT]++; } } int blackPoint; if (!estimateBlackPoint(localBuckets, out blackPoint)) return null; // We delay reading the entire image luminance until the black point estimation succeeds. // Although we end up reading four rows twice, it is consistent with our motto of // "fail quickly" which is necessary for continuous scanning. localLuminances = source.Matrix; for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { int pixel = localLuminances[offset + x] & 0xff; matrix[x, y] = (pixel < blackPoint); } } return matrix; } } /// /// Creates a new object with the same type as this Binarizer implementation, but with pristine /// state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache /// of 1 bit data. See Effective Java for why we can't use Java's clone() method. /// /// The LuminanceSource this Binarizer will operate on. /// /// A new concrete Binarizer implementation object. /// public override Binarizer createBinarizer(LuminanceSource source) { return new GlobalHistogramBinarizer(source); } private void initArrays(int luminanceSize) { if (luminances.Length < luminanceSize) { luminances = new byte[luminanceSize]; } for (int x = 0; x < LUMINANCE_BUCKETS; x++) { buckets[x] = 0; } } private static bool estimateBlackPoint(int[] buckets, out int blackPoint) { blackPoint = 0; // Find the tallest peak in the histogram. int numBuckets = buckets.Length; int maxBucketCount = 0; int firstPeak = 0; int firstPeakSize = 0; for (int x = 0; x < numBuckets; x++) { if (buckets[x] > firstPeakSize) { firstPeak = x; firstPeakSize = buckets[x]; } if (buckets[x] > maxBucketCount) { maxBucketCount = buckets[x]; } } // Find the second-tallest peak which is somewhat far from the tallest peak. int secondPeak = 0; int secondPeakScore = 0; for (int x = 0; x < numBuckets; x++) { int distanceToBiggest = x - firstPeak; // Encourage more distant second peaks by multiplying by square of distance. int score = buckets[x] * distanceToBiggest * distanceToBiggest; if (score > secondPeakScore) { secondPeak = x; secondPeakScore = score; } } // Make sure firstPeak corresponds to the black peak. if (firstPeak > secondPeak) { int temp = firstPeak; firstPeak = secondPeak; secondPeak = temp; } // If there is too little contrast in the image to pick a meaningful black point, throw rather // than waste time trying to decode the image, and risk false positives. // TODO: It might be worth comparing the brightest and darkest pixels seen, rather than the // two peaks, to determine the contrast. if (secondPeak - firstPeak <= numBuckets >> 4) { return false; } // Find a valley between them that is low and closer to the white peak. int bestValley = secondPeak - 1; int bestValleyScore = -1; for (int x = secondPeak - 1; x > firstPeak; x--) { int fromFirst = x - firstPeak; int score = fromFirst*fromFirst*(secondPeak - x)*(maxBucketCount - buckets[x]); if (score > bestValleyScore) { bestValley = x; bestValleyScore = score; } } blackPoint = bestValley << LUMINANCE_SHIFT; return true; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/GridSampler.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common { /// Implementations of this class can, given locations of finder patterns for a QR code in an /// image, sample the right points in the image to reconstruct the QR code, accounting for /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced /// Imaging library, but which may not be available in other environments such as J2ME, and vice /// versa. /// /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)} /// with an instance of a class which implements this interface. /// /// /// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public abstract class GridSampler { /// the current implementation of {@link GridSampler} /// public static GridSampler Instance { get { return gridSampler; } } private static GridSampler gridSampler = new DefaultGridSampler(); /// Sets the implementation of {@link GridSampler} used by the library. One global /// instance is stored, which may sound problematic. But, the implementation provided /// ought to be appropriate for the entire platform, and all uses of this library /// in the whole lifetime of the JVM. For instance, an Android activity can swap in /// an implementation that takes advantage of native platform libraries. /// /// /// The platform-specific object to install. /// public static void setGridSampler(GridSampler newGridSampler) { if (newGridSampler == null) { throw new System.ArgumentException(); } gridSampler = newGridSampler; } ///

Samples an image for a square matrix of bits of the given dimension. This is used to extract /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode /// may be rotated or perspective-distorted, the caller supplies four points in the source image /// that define known points in the barcode, so that the image may be sampled appropriately.

/// ///

The last eight "from" parameters are four X/Y coordinate pairs of locations of points in /// the image that define some significant points in the image to be sample. For example, /// these may be the location of finder pattern in a QR Code.

/// ///

The first eight "to" parameters are four X/Y coordinate pairs measured in the destination /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from" /// parameters map to.

/// ///

These 16 parameters define the transformation needed to sample the image.

/// ///
/// image to sample /// /// width/height of {@link BitMatrix} to sample from image /// /// {@link BitMatrix} representing a grid of points sampled from the image within a region /// defined by the "from" parameters /// /// ReaderException if image can't be sampled, for example, if the transformation defined /// by the given points is invalid or results in sampling outside the image boundaries /// public abstract BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY); public virtual BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform) { throw new System.NotSupportedException(); } ///

Checks a set of points that have been transformed to sample points on an image against /// the image's dimensions to see if the point are even within the image.

/// ///

This method will actually "nudge" the endpoints back onto the image if they are found to be /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder /// patterns in an image where the QR Code runs all the way to the image border.

/// ///

For efficiency, the method will check points from either end of the line until one is found /// to be within the image. Because the set of points are assumed to be linear, this is valid.

/// ///
/// image into which the points should map /// /// actual points in x1,y1,...,xn,yn form /// protected internal static bool checkAndNudgePoints(BitMatrix image, float[] points) { int width = image.Width; int height = image.Height; // Check and nudge points from start until we see some that are OK: bool nudged = true; for (int offset = 0; offset < points.Length && nudged; offset += 2) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int x = (int)points[offset]; //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int y = (int)points[offset + 1]; if (x < -1 || x > width || y < -1 || y > height) { return false; } nudged = false; if (x == -1) { points[offset] = 0.0f; nudged = true; } else if (x == width) { points[offset] = width - 1; nudged = true; } if (y == -1) { points[offset + 1] = 0.0f; nudged = true; } else if (y == height) { points[offset + 1] = height - 1; nudged = true; } } // Check and nudge points from end: nudged = true; for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int x = (int)points[offset]; //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int y = (int)points[offset + 1]; if (x < -1 || x > width || y < -1 || y > height) { return false; } nudged = false; if (x == -1) { points[offset] = 0.0f; nudged = true; } else if (x == width) { points[offset] = width - 1; nudged = true; } if (y == -1) { points[offset + 1] = 0.0f; nudged = true; } else if (y == height) { points[offset + 1] = height - 1; nudged = true; } } return true; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/HybridBinarizer.cs ================================================ /* * Copyright 2009 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common { /// This class implements a local thresholding algorithm, which while slower than the /// GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for /// high frequency images of barcodes with black data on white backgrounds. For this application, /// it does a much better job than a global blackpoint with severe shadows and gradients. /// However it tends to produce artifacts on lower frequency images and is therefore not /// a good general purpose binarizer for uses outside ZXing. /// /// This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers, /// and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already /// inherently local, and only fails for horizontal gradients. We can revisit that problem later, /// but for now it was not a win to use local blocks for 1D. /// /// This Binarizer is the default for the unit tests and the recommended class for library users. /// /// /// dswitkin@google.com (Daniel Switkin) /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class HybridBinarizer : GlobalHistogramBinarizer { override public BitMatrix BlackMatrix { get { binarizeEntireImage(); return matrix; } } // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels. // So this is the smallest dimension in each axis we can accept. private const int BLOCK_SIZE_POWER = 3; private const int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00 private const int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; // ...0011...11 private const int MINIMUM_DIMENSION = 40; private const int MIN_DYNAMIC_RANGE = 24; private BitMatrix matrix = null; public HybridBinarizer(LuminanceSource source) : base(source) { } public override Binarizer createBinarizer(LuminanceSource source) { return new HybridBinarizer(source); } /// /// Calculates the final BitMatrix once for all requests. This could be called once from the /// constructor instead, but there are some advantages to doing it lazily, such as making /// profiling easier, and not doing heavy lifting when callers don't expect it. /// private void binarizeEntireImage() { if (matrix == null) { LuminanceSource source = LuminanceSource; int width = source.Width; int height = source.Height; if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) { byte[] luminances = source.Matrix; int subWidth = width >> BLOCK_SIZE_POWER; if ((width & BLOCK_SIZE_MASK) != 0) { subWidth++; } int subHeight = height >> BLOCK_SIZE_POWER; if ((height & BLOCK_SIZE_MASK) != 0) { subHeight++; } int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); var newMatrix = new BitMatrix(width, height); calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix); matrix = newMatrix; } else { // If the image is too small, fall back to the global histogram approach. matrix = base.BlackMatrix; } } } /// /// For each 8x8 block in the image, calculate the average black point using a 5x5 grid /// of the blocks around it. Also handles the corner cases (fractional blocks are computed based /// on the last 8 pixels in the row/column which are also used in the previous block). /// /// The luminances. /// Width of the sub. /// Height of the sub. /// The width. /// The height. /// The black points. /// The matrix. private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight, int width, int height, int[][] blackPoints, BitMatrix matrix) { for (int y = 0; y < subHeight; y++) { int yoffset = y << BLOCK_SIZE_POWER; int maxYOffset = height - BLOCK_SIZE; if (yoffset > maxYOffset) { yoffset = maxYOffset; } for (int x = 0; x < subWidth; x++) { int xoffset = x << BLOCK_SIZE_POWER; int maxXOffset = width - BLOCK_SIZE; if (xoffset > maxXOffset) { xoffset = maxXOffset; } int left = cap(x, 2, subWidth - 3); int top = cap(y, 2, subHeight - 3); int sum = 0; for (int z = -2; z <= 2; z++) { int[] blackRow = blackPoints[top + z]; sum += blackRow[left - 2]; sum += blackRow[left - 1]; sum += blackRow[left]; sum += blackRow[left + 1]; sum += blackRow[left + 2]; } int average = sum / 25; thresholdBlock(luminances, xoffset, yoffset, average, width, matrix); } } } private static int cap(int value, int min, int max) { return value < min ? min : value > max ? max : value; } /// /// Applies a single threshold to an 8x8 block of pixels. /// /// The luminances. /// The xoffset. /// The yoffset. /// The threshold. /// The stride. /// The matrix. private static void thresholdBlock(byte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix) { int offset = (yoffset * stride) + xoffset; for (int y = 0; y < BLOCK_SIZE; y++, offset += stride) { for (int x = 0; x < BLOCK_SIZE; x++) { int pixel = luminances[offset + x] & 0xff; // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0. matrix[xoffset + x, yoffset + y] = (pixel <= threshold); } } } /// /// Calculates a single black point for each 8x8 block of pixels and saves it away. /// See the following thread for a discussion of this algorithm: /// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0 /// /// The luminances. /// Width of the sub. /// Height of the sub. /// The width. /// The height. /// private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight, int width, int height) { int[][] blackPoints = new int[subHeight][]; for (int i = 0; i < subHeight; i++) { blackPoints[i] = new int[subWidth]; } for (int y = 0; y < subHeight; y++) { int yoffset = y << BLOCK_SIZE_POWER; int maxYOffset = height - BLOCK_SIZE; if (yoffset > maxYOffset) { yoffset = maxYOffset; } for (int x = 0; x < subWidth; x++) { int xoffset = x << BLOCK_SIZE_POWER; int maxXOffset = width - BLOCK_SIZE; if (xoffset > maxXOffset) { xoffset = maxXOffset; } int sum = 0; int min = 0xFF; int max = 0; for (int yy = 0, offset = yoffset * width + xoffset; yy < BLOCK_SIZE; yy++, offset += width) { for (int xx = 0; xx < BLOCK_SIZE; xx++) { int pixel = luminances[offset + xx] & 0xFF; // still looking for good contrast sum += pixel; if (pixel < min) { min = pixel; } if (pixel > max) { max = pixel; } } // short-circuit min/max tests once dynamic range is met if (max - min > MIN_DYNAMIC_RANGE) { // finish the rest of the rows quickly for (yy++, offset += width; yy < BLOCK_SIZE; yy++, offset += width) { for (int xx = 0; xx < BLOCK_SIZE; xx++) { sum += luminances[offset + xx] & 0xFF; } } } } // The default estimate is the average of the values in the block. int average = sum >> (BLOCK_SIZE_POWER * 2); if (max - min <= MIN_DYNAMIC_RANGE) { // If variation within the block is low, assume this is a block with only light or only // dark pixels. In that case we do not want to use the average, as it would divide this // low contrast area into black and white pixels, essentially creating data out of noise. // // The default assumption is that the block is light/background. Since no estimate for // the level of dark pixels exists locally, use half the min for the block. average = min >> 1; if (y > 0 && x > 0) { // Correct the "white background" assumption for blocks that have neighbors by comparing // the pixels in this block to the previously calculated black points. This is based on // the fact that dark barcode symbology is always surrounded by some amount of light // background for which reasonable black point estimates were made. The bp estimated at // the boundaries is used for the interior. // The (min < bp) is arbitrary but works better than other heuristics that were tried. int averageNeighborBlackPoint = (blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) + blackPoints[y - 1][x - 1]) >> 2; if (min < averageNeighborBlackPoint) { average = averageNeighborBlackPoint; } } } blackPoints[y][x] = average; } } return blackPoints; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/PerspectiveTransform.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common { ///

This class implements a perspective transform in two dimensions. Given four source and four /// destination points, it will compute the transformation implied between them. The code is based /// directly upon section 3.4.2 of George Wolberg's "Digital Image Warping"; see pages 54-56.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class PerspectiveTransform { private float a11; private float a12; private float a13; private float a21; private float a22; private float a23; private float a31; private float a32; private float a33; private PerspectiveTransform(float a11, float a21, float a31, float a12, float a22, float a32, float a13, float a23, float a33) { this.a11 = a11; this.a12 = a12; this.a13 = a13; this.a21 = a21; this.a22 = a22; this.a23 = a23; this.a31 = a31; this.a32 = a32; this.a33 = a33; } public static PerspectiveTransform quadrilateralToQuadrilateral(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float x0p, float y0p, float x1p, float y1p, float x2p, float y2p, float x3p, float y3p) { PerspectiveTransform qToS = quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3); PerspectiveTransform sToQ = squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p); return sToQ.times(qToS); } public void transformPoints(float[] points) { int max = points.Length; float a11 = this.a11; float a12 = this.a12; float a13 = this.a13; float a21 = this.a21; float a22 = this.a22; float a23 = this.a23; float a31 = this.a31; float a32 = this.a32; float a33 = this.a33; for (int i = 0; i < max; i += 2) { float x = points[i]; float y = points[i + 1]; float denominator = a13 * x + a23 * y + a33; points[i] = (a11 * x + a21 * y + a31) / denominator; points[i + 1] = (a12 * x + a22 * y + a32) / denominator; } } /// Convenience method, not optimized for performance. public void transformPoints(float[] xValues, float[] yValues) { int n = xValues.Length; for (int i = 0; i < n; i++) { float x = xValues[i]; float y = yValues[i]; float denominator = a13 * x + a23 * y + a33; xValues[i] = (a11 * x + a21 * y + a31) / denominator; yValues[i] = (a12 * x + a22 * y + a32) / denominator; } } public static PerspectiveTransform squareToQuadrilateral(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) { float dx3 = x0 - x1 + x2 - x3; float dy3 = y0 - y1 + y2 - y3; if (dx3 == 0.0f && dy3 == 0.0f) { // Affine return new PerspectiveTransform(x1 - x0, x2 - x1, x0, y1 - y0, y2 - y1, y0, 0.0f, 0.0f, 1.0f); } else { float dx1 = x1 - x2; float dx2 = x3 - x2; float dy1 = y1 - y2; float dy2 = y3 - y2; float denominator = dx1*dy2 - dx2*dy1; float a13 = (dx3*dy2 - dx2*dy3)/denominator; float a23 = (dx1*dy3 - dx3*dy1)/denominator; return new PerspectiveTransform(x1 - x0 + a13*x1, x3 - x0 + a23*x3, x0, y1 - y0 + a13*y1, y3 - y0 + a23*y3, y0, a13, a23, 1.0f); } } public static PerspectiveTransform quadrilateralToSquare(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) { // Here, the adjoint serves as the inverse: return squareToQuadrilateral(x0, y0, x1, y1, x2, y2, x3, y3).buildAdjoint(); } internal PerspectiveTransform buildAdjoint() { // Adjoint is the transpose of the cofactor matrix: return new PerspectiveTransform(a22 * a33 - a23 * a32, a23 * a31 - a21 * a33, a21 * a32 - a22 * a31, a13 * a32 - a12 * a33, a11 * a33 - a13 * a31, a12 * a31 - a11 * a32, a12 * a23 - a13 * a22, a13 * a21 - a11 * a23, a11 * a22 - a12 * a21); } internal PerspectiveTransform times(PerspectiveTransform other) { return new PerspectiveTransform(a11 * other.a11 + a21 * other.a12 + a31 * other.a13, a11 * other.a21 + a21 * other.a22 + a31 * other.a23, a11 * other.a31 + a21 * other.a32 + a31 * other.a33, a12 * other.a11 + a22 * other.a12 + a32 * other.a13, a12 * other.a21 + a22 * other.a22 + a32 * other.a23, a12 * other.a31 + a22 * other.a32 + a32 * other.a33, a13 * other.a11 + a23 * other.a12 + a33 * other.a13, a13 * other.a21 + a23 * other.a22 + a33 * other.a23, a13 * other.a31 + a23 * other.a32 + a33 * other.a33); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/StringUtils.cs ================================================ /* * Copyright (C) 2010 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using System.Text; namespace ZXing.Common { /// /// Common string-related functions. /// /// Sean Owen /// Alex Dupre public static class StringUtils { #if (WINDOWS_PHONE70 || WINDOWS_PHONE71 || WINDOWS_PHONE80 || SILVERLIGHT4 || SILVERLIGHT5 || NETFX_CORE || PORTABLE) private const String PLATFORM_DEFAULT_ENCODING = "UTF-8"; #else private static String PLATFORM_DEFAULT_ENCODING = Encoding.Default.WebName; #endif public static String SHIFT_JIS = "SJIS"; public static String GB2312 = "GB2312"; private const String EUC_JP = "EUC-JP"; private const String UTF8 = "UTF-8"; private const String ISO88591 = "ISO-8859-1"; private static readonly bool ASSUME_SHIFT_JIS = String.Compare(SHIFT_JIS, PLATFORM_DEFAULT_ENCODING, StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(EUC_JP, PLATFORM_DEFAULT_ENCODING, StringComparison.OrdinalIgnoreCase) == 0; /// /// Guesses the encoding. /// /// bytes encoding a string, whose encoding should be guessed /// decode hints if applicable /// name of guessed encoding; at the moment will only guess one of: /// {@link #SHIFT_JIS}, {@link #UTF8}, {@link #ISO88591}, or the platform /// default encoding if none of these can possibly be correct public static String guessEncoding(byte[] bytes, IDictionary hints) { if (hints != null && hints.ContainsKey(DecodeHintType.CHARACTER_SET)) { String characterSet = (String)hints[DecodeHintType.CHARACTER_SET]; if (characterSet != null) { return characterSet; } } // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS, // which should be by far the most common encodings. int length = bytes.Length; bool canBeISO88591 = true; bool canBeShiftJIS = true; bool canBeUTF8 = true; int utf8BytesLeft = 0; //int utf8LowChars = 0; int utf2BytesChars = 0; int utf3BytesChars = 0; int utf4BytesChars = 0; int sjisBytesLeft = 0; //int sjisLowChars = 0; int sjisKatakanaChars = 0; //int sjisDoubleBytesChars = 0; int sjisCurKatakanaWordLength = 0; int sjisCurDoubleBytesWordLength = 0; int sjisMaxKatakanaWordLength = 0; int sjisMaxDoubleBytesWordLength = 0; //int isoLowChars = 0; //int isoHighChars = 0; int isoHighOther = 0; bool utf8bom = bytes.Length > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF; for (int i = 0; i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8); i++) { int value = bytes[i] & 0xFF; // UTF-8 stuff if (canBeUTF8) { if (utf8BytesLeft > 0) { if ((value & 0x80) == 0) { canBeUTF8 = false; } else { utf8BytesLeft--; } } else if ((value & 0x80) != 0) { if ((value & 0x40) == 0) { canBeUTF8 = false; } else { utf8BytesLeft++; if ((value & 0x20) == 0) { utf2BytesChars++; } else { utf8BytesLeft++; if ((value & 0x10) == 0) { utf3BytesChars++; } else { utf8BytesLeft++; if ((value & 0x08) == 0) { utf4BytesChars++; } else { canBeUTF8 = false; } } } } } //else { //utf8LowChars++; //} } // ISO-8859-1 stuff if (canBeISO88591) { if (value > 0x7F && value < 0xA0) { canBeISO88591 = false; } else if (value > 0x9F) { if (value < 0xC0 || value == 0xD7 || value == 0xF7) { isoHighOther++; } //else { //isoHighChars++; //} } //else { //isoLowChars++; //} } // Shift_JIS stuff if (canBeShiftJIS) { if (sjisBytesLeft > 0) { if (value < 0x40 || value == 0x7F || value > 0xFC) { canBeShiftJIS = false; } else { sjisBytesLeft--; } } else if (value == 0x80 || value == 0xA0 || value > 0xEF) { canBeShiftJIS = false; } else if (value > 0xA0 && value < 0xE0) { sjisKatakanaChars++; sjisCurDoubleBytesWordLength = 0; sjisCurKatakanaWordLength++; if (sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength) { sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength; } } else if (value > 0x7F) { sjisBytesLeft++; //sjisDoubleBytesChars++; sjisCurKatakanaWordLength = 0; sjisCurDoubleBytesWordLength++; if (sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength) { sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength; } } else { //sjisLowChars++; sjisCurKatakanaWordLength = 0; sjisCurDoubleBytesWordLength = 0; } } } if (canBeUTF8 && utf8BytesLeft > 0) { canBeUTF8 = false; } if (canBeShiftJIS && sjisBytesLeft > 0) { canBeShiftJIS = false; } // Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done if (canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0)) { return UTF8; } // Easy -- if assuming Shift_JIS or at least 3 valid consecutive not-ascii characters (and no evidence it can't be), done if (canBeShiftJIS && (ASSUME_SHIFT_JIS || sjisMaxKatakanaWordLength >= 3 || sjisMaxDoubleBytesWordLength >= 3)) { return SHIFT_JIS; } // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough for short words. The crude heuristic is: // - If we saw // - only two consecutive katakana chars in the whole text, or // - at least 10% of bytes that could be "upper" not-alphanumeric Latin1, // - then we conclude Shift_JIS, else ISO-8859-1 if (canBeISO88591 && canBeShiftJIS) { return (sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2) || isoHighOther * 10 >= length ? SHIFT_JIS : ISO88591; } // Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding if (canBeISO88591) { return ISO88591; } if (canBeShiftJIS) { return SHIFT_JIS; } if (canBeUTF8) { return UTF8; } // Otherwise, we take a wild guess with platform encoding return PLATFORM_DEFAULT_ENCODING; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/detector/MathUtils.cs ================================================ /* * Copyright 2012 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common.Detector { public static class MathUtils { /// /// Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its /// argument to the nearest int, where x.5 rounds up to x+1. /// /// The d. /// public static int round(float d) { return (int)(d + 0.5f); } public static float distance(float aX, float aY, float bX, float bY) { float xDiff = aX - bX; float yDiff = aY - bY; return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff); } public static float distance(int aX, int aY, int bX, int bY) { int xDiff = aX - bX; int yDiff = aY - bY; return (float)Math.Sqrt(xDiff * xDiff + yDiff * yDiff); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGF.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.Common.ReedSolomon { /// ///

This class contains utility methods for performing mathematical operations over /// the Galois Fields. Operations use a given primitive polynomial in calculations.

///

Throughout this package, elements of the GF are represented as an {@code int} /// for convenience and speed (but at the cost of memory). ///

///
/// Sean Owen public sealed class GenericGF { public static GenericGF AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1 public static GenericGF AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1 public static GenericGF AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1 public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1 public static GenericGF QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1 public static GenericGF DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1 public static GenericGF AZTEC_DATA_8 = DATA_MATRIX_FIELD_256; public static GenericGF MAXICODE_FIELD_64 = AZTEC_DATA_6; private const int INITIALIZATION_THRESHOLD = 0; private int[] expTable; private int[] logTable; private GenericGFPoly zero; private GenericGFPoly one; private readonly int size; private readonly int primitive; private readonly int generatorBase; private bool initialized = false; /// /// Create a representation of GF(size) using the given primitive polynomial. /// /// irreducible polynomial whose coefficients are represented by /// * the bits of an int, where the least-significant bit represents the constant /// * coefficient /// the size of the field /// the factor b in the generator polynomial can be 0- or 1-based /// * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))). /// * In most cases it should be 1, but for QR code it is 0. public GenericGF(int primitive, int size, int genBase) { this.primitive = primitive; this.size = size; this.generatorBase = genBase; if (size <= INITIALIZATION_THRESHOLD) { initialize(); } } private void initialize() { expTable = new int[size]; logTable = new int[size]; int x = 1; for (int i = 0; i < size; i++) { expTable[i] = x; x <<= 1; // x = x * 2; we're assuming the generator alpha is 2 if (x >= size) { x ^= primitive; x &= size - 1; } } for (int i = 0; i < size - 1; i++) { logTable[expTable[i]] = i; } // logTable[0] == 0 but this should never be used zero = new GenericGFPoly(this, new int[] { 0 }); one = new GenericGFPoly(this, new int[] { 1 }); initialized = true; } private void checkInit() { if (!initialized) { initialize(); } } internal GenericGFPoly Zero { get { checkInit(); return zero; } } internal GenericGFPoly One { get { checkInit(); return one; } } /// /// Builds the monomial. /// /// The degree. /// The coefficient. /// the monomial representing coefficient * x^degree internal GenericGFPoly buildMonomial(int degree, int coefficient) { checkInit(); if (degree < 0) { throw new ArgumentException(); } if (coefficient == 0) { return zero; } int[] coefficients = new int[degree + 1]; coefficients[0] = coefficient; return new GenericGFPoly(this, coefficients); } /// /// Implements both addition and subtraction -- they are the same in GF(size). /// /// sum/difference of a and b static internal int addOrSubtract(int a, int b) { return a ^ b; } /// /// Exps the specified a. /// /// 2 to the power of a in GF(size) internal int exp(int a) { checkInit(); return expTable[a]; } /// /// Logs the specified a. /// /// A. /// base 2 log of a in GF(size) internal int log(int a) { checkInit(); if (a == 0) { throw new ArgumentException(); } return logTable[a]; } /// /// Inverses the specified a. /// /// multiplicative inverse of a internal int inverse(int a) { checkInit(); if (a == 0) { throw new ArithmeticException(); } return expTable[size - logTable[a] - 1]; } /// /// Multiplies the specified a with b. /// /// A. /// The b. /// product of a and b in GF(size) internal int multiply(int a, int b) { checkInit(); if (a == 0 || b == 0) { return 0; } return expTable[(logTable[a] + logTable[b]) % (size - 1)]; } /// /// Gets the size. /// public int Size { get { return size; } } /// /// Gets the generator base. /// public int GeneratorBase { get { return generatorBase; } } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// override public String ToString() { return "GF(0x" + primitive.ToString("X") + ',' + size + ')'; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/reedsolomon/GenericGFPoly.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Text; namespace ZXing.Common.ReedSolomon { /// ///

Represents a polynomial whose coefficients are elements of a GF. /// Instances of this class are immutable.

///

Much credit is due to William Rucklidge since portions of this code are an indirect /// port of his C++ Reed-Solomon implementation.

///
/// Sean Owen internal sealed class GenericGFPoly { private readonly GenericGF field; private readonly int[] coefficients; /// /// Initializes a new instance of the class. /// /// the {@link GenericGF} instance representing the field to use /// to perform computations /// coefficients as ints representing elements of GF(size), arranged /// from most significant (highest-power term) coefficient to least significant /// if argument is null or empty, /// or if leading coefficient is 0 and this is not a /// constant polynomial (that is, it is not the monomial "0") internal GenericGFPoly(GenericGF field, int[] coefficients) { if (coefficients.Length == 0) { throw new ArgumentException(); } this.field = field; int coefficientsLength = coefficients.Length; if (coefficientsLength > 1 && coefficients[0] == 0) { // Leading term must be non-zero for anything except the constant polynomial "0" int firstNonZero = 1; while (firstNonZero < coefficientsLength && coefficients[firstNonZero] == 0) { firstNonZero++; } if (firstNonZero == coefficientsLength) { this.coefficients = field.Zero.coefficients; } else { this.coefficients = new int[coefficientsLength - firstNonZero]; Array.Copy(coefficients, firstNonZero, this.coefficients, 0, this.coefficients.Length); } } else { this.coefficients = coefficients; } } internal int[] Coefficients { get { return coefficients; } } /// /// degree of this polynomial /// internal int Degree { get { return coefficients.Length - 1; } } /// /// Gets a value indicating whether this is zero. /// /// true iff this polynomial is the monomial "0" internal bool isZero { get { return coefficients[0] == 0; } } /// /// coefficient of x^degree term in this polynomial /// /// The degree. /// coefficient of x^degree term in this polynomial internal int getCoefficient(int degree) { return coefficients[coefficients.Length - 1 - degree]; } /// /// evaluation of this polynomial at a given point /// /// A. /// evaluation of this polynomial at a given point internal int evaluateAt(int a) { int result = 0; if (a == 0) { // Just return the x^0 coefficient return getCoefficient(0); } int size = coefficients.Length; if (a == 1) { // Just the sum of the coefficients foreach (var coefficient in coefficients) { result = GenericGF.addOrSubtract(result, coefficient); } return result; } result = coefficients[0]; for (int i = 1; i < size; i++) { result = GenericGF.addOrSubtract(field.multiply(a, result), coefficients[i]); } return result; } internal GenericGFPoly addOrSubtract(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (isZero) { return other; } if (other.isZero) { return this; } int[] smallerCoefficients = this.coefficients; int[] largerCoefficients = other.coefficients; if (smallerCoefficients.Length > largerCoefficients.Length) { int[] temp = smallerCoefficients; smallerCoefficients = largerCoefficients; largerCoefficients = temp; } int[] sumDiff = new int[largerCoefficients.Length]; int lengthDiff = largerCoefficients.Length - smallerCoefficients.Length; // Copy high-order terms only found in higher-degree polynomial's coefficients Array.Copy(largerCoefficients, 0, sumDiff, 0, lengthDiff); for (int i = lengthDiff; i < largerCoefficients.Length; i++) { sumDiff[i] = GenericGF.addOrSubtract(smallerCoefficients[i - lengthDiff], largerCoefficients[i]); } return new GenericGFPoly(field, sumDiff); } internal GenericGFPoly multiply(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (isZero || other.isZero) { return field.Zero; } int[] aCoefficients = this.coefficients; int aLength = aCoefficients.Length; int[] bCoefficients = other.coefficients; int bLength = bCoefficients.Length; int[] product = new int[aLength + bLength - 1]; for (int i = 0; i < aLength; i++) { int aCoeff = aCoefficients[i]; for (int j = 0; j < bLength; j++) { product[i + j] = GenericGF.addOrSubtract(product[i + j], field.multiply(aCoeff, bCoefficients[j])); } } return new GenericGFPoly(field, product); } internal GenericGFPoly multiply(int scalar) { if (scalar == 0) { return field.Zero; } if (scalar == 1) { return this; } int size = coefficients.Length; int[] product = new int[size]; for (int i = 0; i < size; i++) { product[i] = field.multiply(coefficients[i], scalar); } return new GenericGFPoly(field, product); } internal GenericGFPoly multiplyByMonomial(int degree, int coefficient) { if (degree < 0) { throw new ArgumentException(); } if (coefficient == 0) { return field.Zero; } int size = coefficients.Length; int[] product = new int[size + degree]; for (int i = 0; i < size; i++) { product[i] = field.multiply(coefficients[i], coefficient); } return new GenericGFPoly(field, product); } internal GenericGFPoly[] divide(GenericGFPoly other) { if (!field.Equals(other.field)) { throw new ArgumentException("GenericGFPolys do not have same GenericGF field"); } if (other.isZero) { throw new ArgumentException("Divide by 0"); } GenericGFPoly quotient = field.Zero; GenericGFPoly remainder = this; int denominatorLeadingTerm = other.getCoefficient(other.Degree); int inverseDenominatorLeadingTerm = field.inverse(denominatorLeadingTerm); while (remainder.Degree >= other.Degree && !remainder.isZero) { int degreeDifference = remainder.Degree - other.Degree; int scale = field.multiply(remainder.getCoefficient(remainder.Degree), inverseDenominatorLeadingTerm); GenericGFPoly term = other.multiplyByMonomial(degreeDifference, scale); GenericGFPoly iterationQuotient = field.buildMonomial(degreeDifference, scale); quotient = quotient.addOrSubtract(iterationQuotient); remainder = remainder.addOrSubtract(term); } return new GenericGFPoly[] { quotient, remainder }; } public override String ToString() { StringBuilder result = new StringBuilder(8 * Degree); for (int degree = Degree; degree >= 0; degree--) { int coefficient = getCoefficient(degree); if (coefficient != 0) { if (coefficient < 0) { result.Append(" - "); coefficient = -coefficient; } else { if (result.Length > 0) { result.Append(" + "); } } if (degree == 0 || coefficient != 1) { int alphaPower = field.log(coefficient); if (alphaPower == 0) { result.Append('1'); } else if (alphaPower == 1) { result.Append('a'); } else { result.Append("a^"); result.Append(alphaPower); } } if (degree != 0) { if (degree == 1) { result.Append('x'); } else { result.Append("x^"); result.Append(degree); } } } } return result.ToString(); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonDecoder.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.Common.ReedSolomon { ///

Implements Reed-Solomon decoding, as the name implies.

/// ///

The algorithm will not be explained here, but the following references were helpful /// in creating this implementation:

/// /// /// ///

Much credit is due to William Rucklidge since portions of this code are an indirect /// port of his C++ Reed-Solomon implementation.

/// ///
/// Sean Owen /// William Rucklidge /// sanfordsquires public sealed class ReedSolomonDecoder { private readonly GenericGF field; public ReedSolomonDecoder(GenericGF field) { this.field = field; } /// ///

Decodes given set of received codewords, which include both data and error-correction /// codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place, /// in the input.

///
/// data and error-correction codewords /// number of error-correction codewords available /// false: decoding fails public bool decode(int[] received, int twoS) { var poly = new GenericGFPoly(field, received); var syndromeCoefficients = new int[twoS]; var noError = true; for (var i = 0; i < twoS; i++) { var eval = poly.evaluateAt(field.exp(i + field.GeneratorBase)); syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval; if (eval != 0) { noError = false; } } if (noError) { return true; } var syndrome = new GenericGFPoly(field, syndromeCoefficients); var sigmaOmega = runEuclideanAlgorithm(field.buildMonomial(twoS, 1), syndrome, twoS); if (sigmaOmega == null) return false; var sigma = sigmaOmega[0]; var errorLocations = findErrorLocations(sigma); if (errorLocations == null) return false; var omega = sigmaOmega[1]; var errorMagnitudes = findErrorMagnitudes(omega, errorLocations); for (var i = 0; i < errorLocations.Length; i++) { var position = received.Length - 1 - field.log(errorLocations[i]); if (position < 0) { // throw new ReedSolomonException("Bad error location"); return false; } received[position] = GenericGF.addOrSubtract(received[position], errorMagnitudes[i]); } return true; } internal GenericGFPoly[] runEuclideanAlgorithm(GenericGFPoly a, GenericGFPoly b, int R) { // Assume a's degree is >= b's if (a.Degree < b.Degree) { GenericGFPoly temp = a; a = b; b = temp; } GenericGFPoly rLast = a; GenericGFPoly r = b; GenericGFPoly tLast = field.Zero; GenericGFPoly t = field.One; // Run Euclidean algorithm until r's degree is less than R/2 while (r.Degree >= R / 2) { GenericGFPoly rLastLast = rLast; GenericGFPoly tLastLast = tLast; rLast = r; tLast = t; // Divide rLastLast by rLast, with quotient in q and remainder in r if (rLast.isZero) { // Oops, Euclidean algorithm already terminated? // throw new ReedSolomonException("r_{i-1} was zero"); return null; } r = rLastLast; GenericGFPoly q = field.Zero; int denominatorLeadingTerm = rLast.getCoefficient(rLast.Degree); int dltInverse = field.inverse(denominatorLeadingTerm); while (r.Degree >= rLast.Degree && !r.isZero) { int degreeDiff = r.Degree - rLast.Degree; int scale = field.multiply(r.getCoefficient(r.Degree), dltInverse); q = q.addOrSubtract(field.buildMonomial(degreeDiff, scale)); r = r.addOrSubtract(rLast.multiplyByMonomial(degreeDiff, scale)); } t = q.multiply(tLast).addOrSubtract(tLastLast); if (r.Degree >= rLast.Degree) { // throw new IllegalStateException("Division algorithm failed to reduce polynomial?"); return null; } } int sigmaTildeAtZero = t.getCoefficient(0); if (sigmaTildeAtZero == 0) { // throw new ReedSolomonException("sigmaTilde(0) was zero"); return null; } int inverse = field.inverse(sigmaTildeAtZero); GenericGFPoly sigma = t.multiply(inverse); GenericGFPoly omega = r.multiply(inverse); return new GenericGFPoly[] { sigma, omega }; } private int[] findErrorLocations(GenericGFPoly errorLocator) { // This is a direct application of Chien's search int numErrors = errorLocator.Degree; if (numErrors == 1) { // shortcut return new int[] { errorLocator.getCoefficient(1) }; } int[] result = new int[numErrors]; int e = 0; for (int i = 1; i < field.Size && e < numErrors; i++) { if (errorLocator.evaluateAt(i) == 0) { result[e] = field.inverse(i); e++; } } if (e != numErrors) { // throw new ReedSolomonException("Error locator degree does not match number of roots"); return null; } return result; } private int[] findErrorMagnitudes(GenericGFPoly errorEvaluator, int[] errorLocations) { // This is directly applying Forney's Formula int s = errorLocations.Length; int[] result = new int[s]; for (int i = 0; i < s; i++) { int xiInverse = field.inverse(errorLocations[i]); int denominator = 1; for (int j = 0; j < s; j++) { if (i != j) { //denominator = field.multiply(denominator, // GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); // Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug. // Below is a funny-looking workaround from Steven Parkes int term = field.multiply(errorLocations[j], xiInverse); int termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1; denominator = field.multiply(denominator, termPlus1); // removed in java version, not sure if this is right // denominator = field.multiply(denominator, GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse))); } } result[i] = field.multiply(errorEvaluator.evaluateAt(xiInverse), field.inverse(denominator)); if (field.GeneratorBase != 0) { result[i] = field.multiply(result[i], xiInverse); } } return result; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/common/reedsolomon/ReedSolomonEncoder.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; namespace ZXing.Common.ReedSolomon { /// /// Implements Reed-Solomon encoding, as the name implies. /// /// Sean Owen /// William Rucklidge public sealed class ReedSolomonEncoder { private readonly GenericGF field; private readonly IList cachedGenerators; public ReedSolomonEncoder(GenericGF field) { this.field = field; this.cachedGenerators = new List(); cachedGenerators.Add(new GenericGFPoly(field, new int[] { 1 })); } private GenericGFPoly buildGenerator(int degree) { if (degree >= cachedGenerators.Count) { var lastGenerator = cachedGenerators[cachedGenerators.Count - 1]; for (int d = cachedGenerators.Count; d <= degree; d++) { var nextGenerator = lastGenerator.multiply(new GenericGFPoly(field, new int[] { 1, field.exp(d - 1 + field.GeneratorBase) })); cachedGenerators.Add(nextGenerator); lastGenerator = nextGenerator; } } return cachedGenerators[degree]; } public void encode(int[] toEncode, int ecBytes) { if (ecBytes == 0) { throw new ArgumentException("No error correction bytes"); } var dataBytes = toEncode.Length - ecBytes; if (dataBytes <= 0) { throw new ArgumentException("No data bytes provided"); } var generator = buildGenerator(ecBytes); var infoCoefficients = new int[dataBytes]; Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes); var info = new GenericGFPoly(field, infoCoefficients); info = info.multiplyByMonomial(ecBytes, 1); var remainder = info.divide(generator)[1]; var coefficients = remainder.Coefficients; var numZeroCoefficients = ecBytes - coefficients.Length; for (var i = 0; i < numZeroCoefficients; i++) { toEncode[dataBytes + i] = 0; } Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/QRCodeReader.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using ZXing.Common; using ZXing.QrCode.Internal; namespace ZXing.QrCode { /// /// This implementation can detect and decode QR Codes in an image. /// Sean Owen /// public class QRCodeReader { private static readonly ResultPoint[] NO_POINTS = new ResultPoint[0]; private readonly Decoder decoder = new Decoder(); /// /// Gets the decoder. /// /// protected Decoder getDecoder() { return decoder; } /// /// Locates and decodes a QR code in an image. /// /// a String representing the content encoded by the QR code /// public Result decode(BinaryBitmap image) { return decode(image, null); } /// /// Locates and decodes a barcode in some format within an image. This method also accepts /// hints, each possibly associated to some data, which may help the implementation decode. /// /// image of barcode to decode /// passed as a from /// to arbitrary data. The /// meaning of the data depends upon the hint type. The implementation may or may not do /// anything with these hints. /// /// String which the barcode encodes /// public Result decode(BinaryBitmap image, IDictionary hints) { DecoderResult decoderResult; ResultPoint[] points; if (image == null || image.BlackMatrix == null) { // something is wrong with the image return null; } if (hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE)) { var bits = extractPureBits(image.BlackMatrix); if (bits == null) return null; decoderResult = decoder.decode(bits, hints); points = NO_POINTS; } else { var detectorResult = new Detector(image.BlackMatrix).detect(hints); if (detectorResult == null) return null; decoderResult = decoder.decode(detectorResult.Bits, hints); points = detectorResult.Points; } if (decoderResult == null) return null; // If the code was mirrored: swap the bottom-left and the top-right points. var data = decoderResult.Other as QRCodeDecoderMetaData; if (data != null) { data.applyMirroredCorrection(points); } var result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE); var byteSegments = decoderResult.ByteSegments; if (byteSegments != null) { result.putMetadata(ResultMetadataType.BYTE_SEGMENTS, byteSegments); } var ecLevel = decoderResult.ECLevel; if (ecLevel != null) { result.putMetadata(ResultMetadataType.ERROR_CORRECTION_LEVEL, ecLevel); } if (decoderResult.StructuredAppend) { result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_SEQUENCE, decoderResult.StructuredAppendSequenceNumber); result.putMetadata(ResultMetadataType.STRUCTURED_APPEND_PARITY, decoderResult.StructuredAppendParity); } return result; } /// /// Resets any internal state the implementation has after a decode, to prepare it /// for reuse. /// public void reset() { // do nothing } /// /// This method detects a code in a "pure" image -- that is, pure monochrome image /// which contains only an unrotated, unskewed, image of a code, with some white border /// around it. This is a specialized method that works exceptionally fast in this special /// case. /// /// /// private static BitMatrix extractPureBits(BitMatrix image) { int[] leftTopBlack = image.getTopLeftOnBit(); int[] rightBottomBlack = image.getBottomRightOnBit(); if (leftTopBlack == null || rightBottomBlack == null) { return null; } float moduleSize; if (!QRCodeReader.moduleSize(leftTopBlack, image, out moduleSize)) return null; int top = leftTopBlack[1]; int bottom = rightBottomBlack[1]; int left = leftTopBlack[0]; int right = rightBottomBlack[0]; // Sanity check! if (left >= right || top >= bottom) { return null; } if (bottom - top != right - left) { // Special case, where bottom-right module wasn't black so we found something else in the last row // Assume it's a square, so use height as the width right = left + (bottom - top); } int matrixWidth = (int)Math.Round((right - left + 1) / moduleSize); int matrixHeight = (int)Math.Round((bottom - top + 1) / moduleSize); if (matrixWidth <= 0 || matrixHeight <= 0) { return null; } if (matrixHeight != matrixWidth) { // Only possibly decode square regions return null; } // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. int nudge = (int)(moduleSize / 2.0f); top += nudge; left += nudge; // But careful that this does not sample off the edge int nudgedTooFarRight = left + (int)((matrixWidth - 1) * moduleSize) - (right - 1); if (nudgedTooFarRight > 0) { if (nudgedTooFarRight > nudge) { // Neither way fits; abort return null; } left -= nudgedTooFarRight; } int nudgedTooFarDown = top + (int)((matrixHeight - 1) * moduleSize) - (bottom - 1); if (nudgedTooFarDown > 0) { if (nudgedTooFarDown > nudge) { // Neither way fits; abort return null; } top -= nudgedTooFarDown; } // Now just read off the bits BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight); for (int y = 0; y < matrixHeight; y++) { int iOffset = top + (int)(y * moduleSize); for (int x = 0; x < matrixWidth; x++) { if (image[left + (int)(x * moduleSize), iOffset]) { bits[x, y] = true; } } } return bits; } private static bool moduleSize(int[] leftTopBlack, BitMatrix image, out float msize) { int height = image.Height; int width = image.Width; int x = leftTopBlack[0]; int y = leftTopBlack[1]; bool inBlack = true; int transitions = 0; while (x < width && y < height) { if (inBlack != image[x, y]) { if (++transitions == 5) { break; } inBlack = !inBlack; } x++; y++; } if (x == width || y == height) { msize = 0.0f; return false; } msize = (x - leftTopBlack[0]) / 7.0f; return true; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/BitMatrixParser.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using ZXing.Common; namespace ZXing.QrCode.Internal { /// Sean Owen sealed class BitMatrixParser { private readonly BitMatrix bitMatrix; private Version parsedVersion; private FormatInformation parsedFormatInfo; private bool mirrored; /// {@link BitMatrix} to parse /// ReaderException if dimension is not >= 21 and 1 mod 4 internal static BitMatrixParser createBitMatrixParser(BitMatrix bitMatrix) { int dimension = bitMatrix.Height; if (dimension < 21 || (dimension & 0x03) != 1) { return null; } return new BitMatrixParser(bitMatrix); } private BitMatrixParser(BitMatrix bitMatrix) { // Should only be called from createBitMatrixParser with the important checks before this.bitMatrix = bitMatrix; } ///

Reads format information from one of its two locations within the QR Code.

/// ///
/// {@link FormatInformation} encapsulating the QR Code's format info /// /// ReaderException if both format information locations cannot be parsed as /// the valid encoding of format information /// internal FormatInformation readFormatInformation() { if (parsedFormatInfo != null) { return parsedFormatInfo; } // Read top-left format info bits int formatInfoBits1 = 0; for (int i = 0; i < 6; i++) { formatInfoBits1 = copyBit(i, 8, formatInfoBits1); } // .. and skip a bit in the timing pattern ... formatInfoBits1 = copyBit(7, 8, formatInfoBits1); formatInfoBits1 = copyBit(8, 8, formatInfoBits1); formatInfoBits1 = copyBit(8, 7, formatInfoBits1); // .. and skip a bit in the timing pattern ... for (int j = 5; j >= 0; j--) { formatInfoBits1 = copyBit(8, j, formatInfoBits1); } // Read the top-right/bottom-left pattern too int dimension = bitMatrix.Height; int formatInfoBits2 = 0; int jMin = dimension - 7; for (int j = dimension - 1; j >= jMin; j--) { formatInfoBits2 = copyBit(8, j, formatInfoBits2); } for (int i = dimension - 8; i < dimension; i++) { formatInfoBits2 = copyBit(i, 8, formatInfoBits2); } parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2); if (parsedFormatInfo != null) { return parsedFormatInfo; } return null; } ///

Reads version information from one of its two locations within the QR Code.

/// ///
/// {@link Version} encapsulating the QR Code's version /// /// ReaderException if both version information locations cannot be parsed as /// the valid encoding of version information /// internal Version readVersion() { if (parsedVersion != null) { return parsedVersion; } int dimension = bitMatrix.Height; int provisionalVersion = (dimension - 17) >> 2; if (provisionalVersion <= 6) { return Version.getVersionForNumber(provisionalVersion); } // Read top-right version info: 3 wide by 6 tall int versionBits = 0; int ijMin = dimension - 11; for (int j = 5; j >= 0; j--) { for (int i = dimension - 9; i >= ijMin; i--) { versionBits = copyBit(i, j, versionBits); } } parsedVersion = Version.decodeVersionInformation(versionBits); if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension) { return parsedVersion; } // Hmm, failed. Try bottom left: 6 wide by 3 tall versionBits = 0; for (int i = 5; i >= 0; i--) { for (int j = dimension - 9; j >= ijMin; j--) { versionBits = copyBit(i, j, versionBits); } } parsedVersion = Version.decodeVersionInformation(versionBits); if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension) { return parsedVersion; } return null; } private int copyBit(int i, int j, int versionBits) { bool bit = mirrored ? bitMatrix[j, i] : bitMatrix[i, j]; return bit ? (versionBits << 1) | 0x1 : versionBits << 1; } ///

Reads the bits in the {@link BitMatrix} representing the finder pattern in the /// correct order in order to reconstruct the codewords bytes contained within the /// QR Code.

/// ///
/// bytes encoded within the QR Code /// /// ReaderException if the exact number of bytes expected is not read internal byte[] readCodewords() { FormatInformation formatInfo = readFormatInformation(); if (formatInfo == null) return null; Version version = readVersion(); if (version == null) return null; // Get the data mask for the format used in this QR Code. This will exclude // some bits from reading as we wind through the bit matrix. DataMask dataMask = DataMask.forReference(formatInfo.DataMask); int dimension = bitMatrix.Height; dataMask.unmaskBitMatrix(bitMatrix, dimension); BitMatrix functionPattern = version.buildFunctionPattern(); bool readingUp = true; byte[] result = new byte[version.TotalCodewords]; int resultOffset = 0; int currentByte = 0; int bitsRead = 0; // Read columns in pairs, from right to left for (int j = dimension - 1; j > 0; j -= 2) { if (j == 6) { // Skip whole column with vertical alignment pattern; // saves time and makes the other code proceed more cleanly j--; } // Read alternatingly from bottom to top then top to bottom for (int count = 0; count < dimension; count++) { int i = readingUp ? dimension - 1 - count : count; for (int col = 0; col < 2; col++) { // Ignore bits covered by the function pattern if (!functionPattern[j - col, i]) { // Read a bit bitsRead++; currentByte <<= 1; if (bitMatrix[j - col, i]) { currentByte |= 1; } // If we've made a whole byte, save it off if (bitsRead == 8) { result[resultOffset++] = (byte)currentByte; bitsRead = 0; currentByte = 0; } } } } readingUp ^= true; // readingUp = !readingUp; // switch directions } if (resultOffset != version.TotalCodewords) { return null; } return result; } /** * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state. */ internal void remask() { if (parsedFormatInfo == null) { return; // We have no format information, and have no data mask } DataMask dataMask = DataMask.forReference(parsedFormatInfo.DataMask); int dimension = bitMatrix.Height; dataMask.unmaskBitMatrix(bitMatrix, dimension); } /** * Prepare the parser for a mirrored operation. * This flag has effect only on the {@link #readFormatInformation()} and the * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the * {@link #mirror()} method should be called. * * @param mirror Whether to read version and format information mirrored. */ internal void setMirror(bool mirror) { parsedVersion = null; parsedFormatInfo = null; mirrored = mirror; } /** Mirror the bit matrix in order to attempt a second reading. */ internal void mirror() { for (int x = 0; x < bitMatrix.Width; x++) { for (int y = x + 1; y < bitMatrix.Height; y++) { if (bitMatrix[x, y] != bitMatrix[y, x]) { bitMatrix.flip(y, x); bitMatrix.flip(x, y); } } } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataBlock.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.QrCode.Internal { ///

Encapsulates a block of data within a QR Code. QR Codes may split their data into /// multiple blocks, each of which is a unit of data and error-correction codewords. Each /// is represented by an instance of this class.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// internal sealed class DataBlock { private readonly int numDataCodewords; private readonly byte[] codewords; private DataBlock(int numDataCodewords, byte[] codewords) { this.numDataCodewords = numDataCodewords; this.codewords = codewords; } ///

When QR Codes use multiple data blocks, they are actually interleaved. /// That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This /// method will separate the data into original blocks.

/// ///
/// bytes as read directly from the QR Code /// /// version of the QR Code /// /// error-correction level of the QR Code /// /// {@link DataBlock}s containing original bytes, "de-interleaved" from representation in the /// QR Code /// internal static DataBlock[] getDataBlocks(byte[] rawCodewords, Version version, ErrorCorrectionLevel ecLevel) { if (rawCodewords.Length != version.TotalCodewords) { throw new System.ArgumentException(); } // Figure out the number and size of data blocks used by this version and // error correction level Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); // First count the total number of data blocks int totalBlocks = 0; Version.ECB[] ecBlockArray = ecBlocks.getECBlocks(); foreach (var ecBlock in ecBlockArray) { totalBlocks += ecBlock.Count; } // Now establish DataBlocks of the appropriate size and number of data codewords DataBlock[] result = new DataBlock[totalBlocks]; int numResultBlocks = 0; foreach (var ecBlock in ecBlockArray) { for (int i = 0; i < ecBlock.Count; i++) { int numDataCodewords = ecBlock.DataCodewords; int numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords; result[numResultBlocks++] = new DataBlock(numDataCodewords, new byte[numBlockCodewords]); } } // All blocks have the same amount of data, except that the last n // (where n may be 0) have 1 more byte. Figure out where these start. int shorterBlocksTotalCodewords = result[0].codewords.Length; int longerBlocksStartAt = result.Length - 1; while (longerBlocksStartAt >= 0) { int numCodewords = result[longerBlocksStartAt].codewords.Length; if (numCodewords == shorterBlocksTotalCodewords) { break; } longerBlocksStartAt--; } longerBlocksStartAt++; int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock; // The last elements of result may be 1 element longer; // first fill out as many elements as all of them have int rawCodewordsOffset = 0; for (int i = 0; i < shorterBlocksNumDataCodewords; i++) { for (int j = 0; j < numResultBlocks; j++) { result[j].codewords[i] = rawCodewords[rawCodewordsOffset++]; } } // Fill out the last data block in the longer ones for (int j = longerBlocksStartAt; j < numResultBlocks; j++) { result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++]; } // Now add in error correction blocks int max = result[0].codewords.Length; for (int i = shorterBlocksNumDataCodewords; i < max; i++) { for (int j = 0; j < numResultBlocks; j++) { int iOffset = j < longerBlocksStartAt ? i : i + 1; result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++]; } } return result; } internal int NumDataCodewords { get { return numDataCodewords; } } internal byte[] Codewords { get { return codewords; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/DataMask.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using ZXing.Common; namespace ZXing.QrCode.Internal { ///

Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations /// of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix, /// including areas used for finder patterns, timing patterns, etc. These areas should be unused /// after the point they are unmasked anyway.

/// ///

Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position /// and j is row position. In fact, as the text says, i is row position and j is column position.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// abstract class DataMask { /// See ISO 18004:2006 6.8.1 private static readonly DataMask[] DATA_MASKS = new DataMask[] { new DataMask000(), new DataMask001(), new DataMask010(), new DataMask011(), new DataMask100(), new DataMask101(), new DataMask110(), new DataMask111() }; private DataMask() { } ///

Implementations of this method reverse the data masking process applied to a QR Code and /// make its bits ready to read.

/// ///
/// representation of QR Code bits /// /// dimension of QR Code, represented by bits, being unmasked /// internal void unmaskBitMatrix(BitMatrix bits, int dimension) { for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { if (isMasked(i, j)) { bits.flip(j, i); } } } } internal abstract bool isMasked(int i, int j); /// a value between 0 and 7 indicating one of the eight possible /// data mask patterns a QR Code may use /// /// {@link DataMask} encapsulating the data mask pattern /// internal static DataMask forReference(int reference) { if (reference < 0 || reference > 7) { throw new System.ArgumentException(); } return DATA_MASKS[reference]; } /// 000: mask bits for which (x + y) mod 2 == 0 private sealed class DataMask000 : DataMask { internal override bool isMasked(int i, int j) { return ((i + j) & 0x01) == 0; } } /// 001: mask bits for which x mod 2 == 0 private sealed class DataMask001 : DataMask { internal override bool isMasked(int i, int j) { return (i & 0x01) == 0; } } /// 010: mask bits for which y mod 3 == 0 private sealed class DataMask010 : DataMask { internal override bool isMasked(int i, int j) { return j % 3 == 0; } } /// 011: mask bits for which (x + y) mod 3 == 0 private sealed class DataMask011 : DataMask { internal override bool isMasked(int i, int j) { return (i + j) % 3 == 0; } } /// 100: mask bits for which (x/2 + y/3) mod 2 == 0 private sealed class DataMask100 : DataMask { internal override bool isMasked(int i, int j) { return ((((int)((uint)i >> 1)) + (j / 3)) & 0x01) == 0; } } /// 101: mask bits for which xy mod 2 + xy mod 3 == 0 private sealed class DataMask101 : DataMask { internal override bool isMasked(int i, int j) { int temp = i * j; return (temp & 0x01) + (temp % 3) == 0; } } /// 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0 private sealed class DataMask110 : DataMask { internal override bool isMasked(int i, int j) { int temp = i * j; return (((temp & 0x01) + (temp % 3)) & 0x01) == 0; } } /// 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0 private sealed class DataMask111 : DataMask { internal override bool isMasked(int i, int j) { return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/DecodedBitStreamParser.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using System.Text; using ZXing.Common; namespace ZXing.QrCode.Internal { ///

QR Codes can encode text as bits in one of several modes, and can use multiple modes /// in one QR Code. This class decodes the bits back into text.

/// ///

See ISO 18004:2006, 6.4.3 - 6.4.7

/// Sean Owen ///
internal static class DecodedBitStreamParser { /// /// See ISO 18004:2006, 6.4.4 Table 5 /// private static readonly char[] ALPHANUMERIC_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':' }; private const int GB2312_SUBSET = 1; internal static DecoderResult decode(byte[] bytes, Version version, ErrorCorrectionLevel ecLevel, IDictionary hints) { var bits = new BitSource(bytes); var result = new StringBuilder(50); var byteSegments = new List(1); var symbolSequence = -1; var parityData = -1; try { // CharacterSetECI currentCharacterSetECI = null; bool fc1InEffect = false; Mode mode; do { // While still another segment to read... if (bits.available() < 4) { // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here mode = Mode.TERMINATOR; } else { try { mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits } catch (ArgumentException) { return null; } } if (mode != Mode.TERMINATOR) { if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION) { // We do little with FNC1 except alter the parsed result a bit according to the spec fc1InEffect = true; } else if (mode == Mode.STRUCTURED_APPEND) { if (bits.available() < 16) { return null; } // not really supported; but sequence number and parity is added later to the result metadata // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue symbolSequence = bits.readBits(8); parityData = bits.readBits(8); } else if (mode == Mode.ECI) { /* // Count doesn't apply to ECI int value = parseECIValue(bits); currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value); if (currentCharacterSetECI == null) { return null; } * */ } else { // First handle Hanzi mode which does not start with character count if (mode == Mode.HANZI) { //chinese mode contains a sub set indicator right after mode indicator int subset = bits.readBits(4); int countHanzi = bits.readBits(mode.getCharacterCountBits(version)); if (subset == GB2312_SUBSET) { if (!decodeHanziSegment(bits, result, countHanzi)) return null; } } else { // "Normal" QR code modes: // How many characters will follow, encoded in this mode? int count = bits.readBits(mode.getCharacterCountBits(version)); if (mode == Mode.NUMERIC) { if (!decodeNumericSegment(bits, result, count)) return null; } else if (mode == Mode.ALPHANUMERIC) { if (!decodeAlphanumericSegment(bits, result, count, fc1InEffect)) return null; } else if (mode == Mode.BYTE) { if (!decodeByteSegment(bits, result, count, byteSegments, hints)) return null; } else if (mode == Mode.KANJI) { if (!decodeKanjiSegment(bits, result, count)) return null; } else { return null; } } } } } while (mode != Mode.TERMINATOR); } catch (ArgumentException) { // from readBits() calls return null; } #if WindowsCE var resultString = result.ToString().Replace("\n", "\r\n"); #else var resultString = result.ToString().Replace("\r\n", "\n").Replace("\n", Environment.NewLine); #endif return new DecoderResult(bytes, resultString, byteSegments.Count == 0 ? null : byteSegments, ecLevel == null ? null : ecLevel.ToString(), symbolSequence, parityData); } /// /// See specification GBT 18284-2000 /// /// The bits. /// The result. /// The count. /// private static bool decodeHanziSegment(BitSource bits, StringBuilder result, int count) { // Don't crash trying to read more bits than we have available. if (count * 13 > bits.available()) { return false; } // Each character will require 2 bytes. Read the characters as 2-byte pairs // and decode as GB2312 afterwards byte[] buffer = new byte[2 * count]; int offset = 0; while (count > 0) { // Each 13 bits encodes a 2-byte character int twoBytes = bits.readBits(13); int assembledTwoBytes = ((twoBytes / 0x060) << 8) | (twoBytes % 0x060); if (assembledTwoBytes < 0x003BF) { // In the 0xA1A1 to 0xAAFE range assembledTwoBytes += 0x0A1A1; } else { // In the 0xB0A1 to 0xFAFE range assembledTwoBytes += 0x0A6A1; } buffer[offset] = (byte)((assembledTwoBytes >> 8) & 0xFF); buffer[offset + 1] = (byte)(assembledTwoBytes & 0xFF); offset += 2; count--; } try { result.Append(Encoding.GetEncoding(StringUtils.GB2312).GetString(buffer, 0, buffer.Length)); } #if (WINDOWS_PHONE70 || WINDOWS_PHONE71 || SILVERLIGHT4 || SILVERLIGHT5 || NETFX_CORE || MONOANDROID || MONOTOUCH) catch (ArgumentException) { try { // Silverlight only supports a limited number of character sets, trying fallback to UTF-8 result.Append(Encoding.GetEncoding("UTF-8").GetString(buffer, 0, buffer.Length)); } catch (Exception) { return false; } } #endif catch (Exception) { return false; } return true; } private static bool decodeKanjiSegment(BitSource bits, StringBuilder result, int count) { // Don't crash trying to read more bits than we have available. if (count * 13 > bits.available()) { return false; } // Each character will require 2 bytes. Read the characters as 2-byte pairs // and decode as Shift_JIS afterwards byte[] buffer = new byte[2 * count]; int offset = 0; while (count > 0) { // Each 13 bits encodes a 2-byte character int twoBytes = bits.readBits(13); int assembledTwoBytes = ((twoBytes / 0x0C0) << 8) | (twoBytes % 0x0C0); if (assembledTwoBytes < 0x01F00) { // In the 0x8140 to 0x9FFC range assembledTwoBytes += 0x08140; } else { // In the 0xE040 to 0xEBBF range assembledTwoBytes += 0x0C140; } buffer[offset] = (byte)(assembledTwoBytes >> 8); buffer[offset + 1] = (byte)assembledTwoBytes; offset += 2; count--; } // Shift_JIS may not be supported in some environments: try { result.Append(Encoding.GetEncoding(StringUtils.SHIFT_JIS).GetString(buffer, 0, buffer.Length)); } #if (WINDOWS_PHONE70 || WINDOWS_PHONE71 || SILVERLIGHT4 || SILVERLIGHT5 || NETFX_CORE || MONOANDROID || MONOTOUCH) catch (ArgumentException) { try { // Silverlight only supports a limited number of character sets, trying fallback to UTF-8 result.Append(Encoding.GetEncoding("UTF-8").GetString(buffer, 0, buffer.Length)); } catch (Exception) { return false; } } #endif catch (Exception) { return false; } return true; } private static bool decodeByteSegment(BitSource bits, StringBuilder result, int count, IList byteSegments, IDictionary hints) { // Don't crash trying to read more bits than we have available. if (count << 3 > bits.available()) { return false; } byte[] readBytes = new byte[count]; for (int i = 0; i < count; i++) { readBytes[i] = (byte)bits.readBits(8); } String encoding; encoding = StringUtils.guessEncoding(readBytes, hints); try { result.Append(Encoding.GetEncoding(encoding).GetString(readBytes, 0, readBytes.Length)); } #if (WINDOWS_PHONE70 || WINDOWS_PHONE71 || SILVERLIGHT4 || SILVERLIGHT5 || NETFX_CORE || MONOANDROID || MONOTOUCH) catch (ArgumentException) { try { // Silverlight only supports a limited number of character sets, trying fallback to UTF-8 result.Append(Encoding.GetEncoding("UTF-8").GetString(readBytes, 0, readBytes.Length)); } catch (Exception) { return false; } } #endif #if WindowsCE catch (PlatformNotSupportedException) { try { // WindowsCE doesn't support all encodings. But it is device depended. // So we try here the some different ones if (encoding == "ISO-8859-1") { result.Append(Encoding.GetEncoding(1252).GetString(readBytes, 0, readBytes.Length)); } else { result.Append(Encoding.GetEncoding("UTF-8").GetString(readBytes, 0, readBytes.Length)); } } catch (Exception) { return false; } } #endif catch (Exception) { return false; } byteSegments.Add(readBytes); return true; } private static char toAlphaNumericChar(int value) { if (value >= ALPHANUMERIC_CHARS.Length) { //throw FormatException.Instance; } return ALPHANUMERIC_CHARS[value]; } private static bool decodeAlphanumericSegment(BitSource bits, StringBuilder result, int count, bool fc1InEffect) { // Read two characters at a time int start = result.Length; while (count > 1) { if (bits.available() < 11) { return false; } int nextTwoCharsBits = bits.readBits(11); result.Append(toAlphaNumericChar(nextTwoCharsBits / 45)); result.Append(toAlphaNumericChar(nextTwoCharsBits % 45)); count -= 2; } if (count == 1) { // special case: one character left if (bits.available() < 6) { return false; } result.Append(toAlphaNumericChar(bits.readBits(6))); } // See section 6.4.8.1, 6.4.8.2 if (fc1InEffect) { // We need to massage the result a bit if in an FNC1 mode: for (int i = start; i < result.Length; i++) { if (result[i] == '%') { if (i < result.Length - 1 && result[i + 1] == '%') { // %% is rendered as % result.Remove(i + 1, 1); } else { // In alpha mode, % should be converted to FNC1 separator 0x1D result.Remove(i, 1); result.Insert(i, new[] { (char)0x1D }); } } } } return true; } private static bool decodeNumericSegment(BitSource bits, StringBuilder result, int count) { // Read three digits at a time while (count >= 3) { // Each 10 bits encodes three digits if (bits.available() < 10) { return false; } int threeDigitsBits = bits.readBits(10); if (threeDigitsBits >= 1000) { return false; } result.Append(toAlphaNumericChar(threeDigitsBits / 100)); result.Append(toAlphaNumericChar((threeDigitsBits / 10) % 10)); result.Append(toAlphaNumericChar(threeDigitsBits % 10)); count -= 3; } if (count == 2) { // Two digits left over to read, encoded in 7 bits if (bits.available() < 7) { return false; } int twoDigitsBits = bits.readBits(7); if (twoDigitsBits >= 100) { return false; } result.Append(toAlphaNumericChar(twoDigitsBits / 10)); result.Append(toAlphaNumericChar(twoDigitsBits % 10)); } else if (count == 1) { // One digit left over to read if (bits.available() < 4) { return false; } int digitBits = bits.readBits(4); if (digitBits >= 10) { return false; } result.Append(toAlphaNumericChar(digitBits)); } return true; } private static int parseECIValue(BitSource bits) { int firstByte = bits.readBits(8); if ((firstByte & 0x80) == 0) { // just one byte return firstByte & 0x7F; } if ((firstByte & 0xC0) == 0x80) { // two bytes int secondByte = bits.readBits(8); return ((firstByte & 0x3F) << 8) | secondByte; } if ((firstByte & 0xE0) == 0xC0) { // three bytes int secondThirdBytes = bits.readBits(16); return ((firstByte & 0x1F) << 16) | secondThirdBytes; } throw new ArgumentException("Bad ECI bits starting with byte " + firstByte); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/Decoder.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System.Collections.Generic; using ZXing.Common; using ZXing.Common.ReedSolomon; namespace ZXing.QrCode.Internal { /// ///

The main class which implements QR Code decoding -- as opposed to locating and extracting /// the QR Code from an image.

///
/// /// Sean Owen /// public sealed class Decoder { private readonly ReedSolomonDecoder rsDecoder; /// /// Initializes a new instance of the class. /// public Decoder() { rsDecoder = new ReedSolomonDecoder(GenericGF.QR_CODE_FIELD_256); } /// ///

Convenience method that can decode a QR Code represented as a 2D array of booleans. /// "true" is taken to mean a black module.

///
/// booleans representing white/black QR Code modules /// The hints. /// /// text and bytes encoded within the QR Code /// public DecoderResult decode(bool[][] image, IDictionary hints) { var dimension = image.Length; var bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { bits[j, i] = image[i][j]; } } return decode(bits, hints); } /// ///

Decodes a QR Code represented as a {@link BitMatrix}. A 1 or "true" is taken to mean a black module.

///
/// booleans representing white/black QR Code modules /// The hints. /// /// text and bytes encoded within the QR Code /// public DecoderResult decode(BitMatrix bits, IDictionary hints) { // Construct a parser and read version, error-correction level var parser = BitMatrixParser.createBitMatrixParser(bits); if (parser == null) return null; var result = decode(parser, hints); if (result == null) { // Revert the bit matrix parser.remask(); // Will be attempting a mirrored reading of the version and format info. parser.setMirror(true); // Preemptively read the version. var version = parser.readVersion(); if (version == null) return null; // Preemptively read the format information. var formatinfo = parser.readFormatInformation(); if (formatinfo == null) return null; /* * Since we're here, this means we have successfully detected some kind * of version and format information when mirrored. This is a good sign, * that the QR code may be mirrored, and we should try once more with a * mirrored content. */ // Prepare for a mirrored reading. parser.mirror(); result = decode(parser, hints); if (result != null) { // Success! Notify the caller that the code was mirrored. result.Other = new QRCodeDecoderMetaData(true); } } return result; } private DecoderResult decode(BitMatrixParser parser, IDictionary hints) { Version version = parser.readVersion(); if (version == null) return null; var formatinfo = parser.readFormatInformation(); if (formatinfo == null) return null; ErrorCorrectionLevel ecLevel = formatinfo.ErrorCorrectionLevel; // Read codewords byte[] codewords = parser.readCodewords(); if (codewords == null) return null; // Separate into data blocks DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version, ecLevel); // Count total number of data bytes int totalBytes = 0; foreach (var dataBlock in dataBlocks) { totalBytes += dataBlock.NumDataCodewords; } byte[] resultBytes = new byte[totalBytes]; int resultOffset = 0; // Error-correct and copy data blocks together into a stream of bytes foreach (var dataBlock in dataBlocks) { byte[] codewordBytes = dataBlock.Codewords; int numDataCodewords = dataBlock.NumDataCodewords; if (!correctErrors(codewordBytes, numDataCodewords)) return null; for (int i = 0; i < numDataCodewords; i++) { resultBytes[resultOffset++] = codewordBytes[i]; } } // Decode the contents of that stream of bytes return DecodedBitStreamParser.decode(resultBytes, version, ecLevel, hints); } /// ///

Given data and error-correction codewords received, possibly corrupted by errors, attempts to /// correct the errors in-place using Reed-Solomon error correction.

///
/// data and error correction codewords /// number of codewords that are data bytes /// private bool correctErrors(byte[] codewordBytes, int numDataCodewords) { int numCodewords = codewordBytes.Length; // First read into an array of ints int[] codewordsInts = new int[numCodewords]; for (int i = 0; i < numCodewords; i++) { codewordsInts[i] = codewordBytes[i] & 0xFF; } int numECCodewords = codewordBytes.Length - numDataCodewords; if (!rsDecoder.decode(codewordsInts, numECCodewords)) return false; // Copy back into array of bytes -- only need to worry about the bytes that were data // We don't care about errors in the error-correction codewords for (int i = 0; i < numDataCodewords; i++) { codewordBytes[i] = (byte)codewordsInts[i]; } return true; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/ErrorCorrectionLevel.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { /// ///

See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels /// defined by the QR code standard.

///
/// Sean Owen public sealed class ErrorCorrectionLevel { /// L = ~7% correction public static readonly ErrorCorrectionLevel L = new ErrorCorrectionLevel(0, 0x01, "L"); /// M = ~15% correction public static readonly ErrorCorrectionLevel M = new ErrorCorrectionLevel(1, 0x00, "M"); /// Q = ~25% correction public static readonly ErrorCorrectionLevel Q = new ErrorCorrectionLevel(2, 0x03, "Q"); /// H = ~30% correction public static readonly ErrorCorrectionLevel H = new ErrorCorrectionLevel(3, 0x02, "H"); private static readonly ErrorCorrectionLevel[] FOR_BITS = new [] { M, L, H, Q }; private readonly int bits; private ErrorCorrectionLevel(int ordinal, int bits, String name) { this.ordinal_Renamed_Field = ordinal; this.bits = bits; this.name = name; } /// /// Gets the bits. /// public int Bits { get { return bits; } } /// /// Gets the name. /// public String Name { get { return name; } } private readonly int ordinal_Renamed_Field; private readonly String name; /// /// Ordinals this instance. /// /// public int ordinal() { return ordinal_Renamed_Field; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { return name; } /// /// Fors the bits. /// /// int containing the two bits encoding a QR Code's error correction level /// /// representing the encoded error correction level /// public static ErrorCorrectionLevel forBits(int bits) { if (bits < 0 || bits >= FOR_BITS.Length) { throw new ArgumentException(); } return FOR_BITS[bits]; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/FormatInformation.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { ///

Encapsulates a QR Code's format information, including the data mask used and /// error correction level.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// /// /// /// /// sealed class FormatInformation { private const int FORMAT_INFO_MASK_QR = 0x5412; /// See ISO 18004:2006, Annex C, Table C.1 private static readonly int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][] { new [] { 0x5412, 0x00 }, new [] { 0x5125, 0x01 }, new [] { 0x5E7C, 0x02 }, new [] { 0x5B4B, 0x03 }, new [] { 0x45F9, 0x04 }, new [] { 0x40CE, 0x05 }, new [] { 0x4F97, 0x06 }, new [] { 0x4AA0, 0x07 }, new [] { 0x77C4, 0x08 }, new [] { 0x72F3, 0x09 }, new [] { 0x7DAA, 0x0A }, new [] { 0x789D, 0x0B }, new [] { 0x662F, 0x0C }, new [] { 0x6318, 0x0D }, new [] { 0x6C41, 0x0E }, new [] { 0x6976, 0x0F }, new [] { 0x1689, 0x10 }, new [] { 0x13BE, 0x11 }, new [] { 0x1CE7, 0x12 }, new [] { 0x19D0, 0x13 }, new [] { 0x0762, 0x14 }, new [] { 0x0255, 0x15 }, new [] { 0x0D0C, 0x16 }, new [] { 0x083B, 0x17 }, new [] { 0x355F, 0x18 }, new [] { 0x3068, 0x19 }, new [] { 0x3F31, 0x1A }, new [] { 0x3A06, 0x1B }, new [] { 0x24B4, 0x1C }, new [] { 0x2183, 0x1D }, new [] { 0x2EDA, 0x1E }, new [] { 0x2BED, 0x1F } }; /// Offset i holds the number of 1 bits in the binary representation of i private static readonly int[] BITS_SET_IN_HALF_BYTE = new [] { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; private readonly ErrorCorrectionLevel errorCorrectionLevel; private readonly byte dataMask; private FormatInformation(int formatInfo) { // Bits 3,4 errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03); // Bottom 3 bits dataMask = (byte)(formatInfo & 0x07); } internal static int numBitsDiffering(int a, int b) { a ^= b; // a now has a 1 bit exactly where its bit differs with b's // Count bits set quickly with a series of lookups: return BITS_SET_IN_HALF_BYTE[a & 0x0F] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 4)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 8)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 12)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 16)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 20)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 24)) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 28)) & 0x0F)]; } /// /// Decodes the format information. /// /// format info indicator, with mask still applied /// The masked format info2. /// /// information about the format it specifies, or null /// if doesn't seem to match any known pattern /// internal static FormatInformation decodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) { FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2); if (formatInfo != null) { return formatInfo; } // Should return null, but, some QR codes apparently // do not mask this info. Try again by actually masking the pattern // first return doDecodeFormatInformation(maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR, maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR); } private static FormatInformation doDecodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2) { // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing int bestDifference = Int32.MaxValue; int bestFormatInfo = 0; foreach (var decodeInfo in FORMAT_INFO_DECODE_LOOKUP) { int targetInfo = decodeInfo[0]; if (targetInfo == maskedFormatInfo1 || targetInfo == maskedFormatInfo2) { // Found an exact match return new FormatInformation(decodeInfo[1]); } int bitsDifference = numBitsDiffering(maskedFormatInfo1, targetInfo); if (bitsDifference < bestDifference) { bestFormatInfo = decodeInfo[1]; bestDifference = bitsDifference; } if (maskedFormatInfo1 != maskedFormatInfo2) { // also try the other option bitsDifference = numBitsDiffering(maskedFormatInfo2, targetInfo); if (bitsDifference < bestDifference) { bestFormatInfo = decodeInfo[1]; bestDifference = bitsDifference; } } } // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits // differing means we found a match if (bestDifference <= 3) { return new FormatInformation(bestFormatInfo); } return null; } internal ErrorCorrectionLevel ErrorCorrectionLevel { get { return errorCorrectionLevel; } } internal byte DataMask { get { return dataMask; } } public override int GetHashCode() { return (errorCorrectionLevel.ordinal() << 3) | dataMask; } public override bool Equals(Object o) { if (!(o is FormatInformation)) { return false; } var other = (FormatInformation)o; return errorCorrectionLevel == other.errorCorrectionLevel && dataMask == other.dataMask; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/Mode.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { /// ///

See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which /// data can be encoded to bits in the QR code standard.

///
/// Sean Owen public sealed class Mode { /// /// Gets the name. /// public String Name { get { return name; } } // No, we can't use an enum here. J2ME doesn't support it. /// /// /// public static readonly Mode TERMINATOR = new Mode(new int[] { 0, 0, 0 }, 0x00, "TERMINATOR"); // Not really a mode... /// /// /// public static readonly Mode NUMERIC = new Mode(new int[] { 10, 12, 14 }, 0x01, "NUMERIC"); /// /// /// public static readonly Mode ALPHANUMERIC = new Mode(new int[] { 9, 11, 13 }, 0x02, "ALPHANUMERIC"); /// /// /// public static readonly Mode STRUCTURED_APPEND = new Mode(new int[] { 0, 0, 0 }, 0x03, "STRUCTURED_APPEND"); // Not supported /// /// /// public static readonly Mode BYTE = new Mode(new int[] { 8, 16, 16 }, 0x04, "BYTE"); /// /// /// public static readonly Mode ECI = new Mode(null, 0x07, "ECI"); // character counts don't apply /// /// /// public static readonly Mode KANJI = new Mode(new int[] { 8, 10, 12 }, 0x08, "KANJI"); /// /// /// public static readonly Mode FNC1_FIRST_POSITION = new Mode(null, 0x05, "FNC1_FIRST_POSITION"); /// /// /// public static readonly Mode FNC1_SECOND_POSITION = new Mode(null, 0x09, "FNC1_SECOND_POSITION"); /// See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. public static readonly Mode HANZI = new Mode(new int[] { 8, 10, 12 }, 0x0D, "HANZI"); private readonly int[] characterCountBitsForVersions; private readonly int bits; private readonly String name; private Mode(int[] characterCountBitsForVersions, int bits, System.String name) { this.characterCountBitsForVersions = characterCountBitsForVersions; this.bits = bits; this.name = name; } /// /// Fors the bits. /// /// four bits encoding a QR Code data mode /// /// encoded by these bits /// /// if bits do not correspond to a known mode public static Mode forBits(int bits) { switch (bits) { case 0x0: return TERMINATOR; case 0x1: return NUMERIC; case 0x2: return ALPHANUMERIC; case 0x3: return STRUCTURED_APPEND; case 0x4: return BYTE; case 0x5: return FNC1_FIRST_POSITION; case 0x7: return ECI; case 0x8: return KANJI; case 0x9: return FNC1_SECOND_POSITION; case 0xD: // 0xD is defined in GBT 18284-2000, may not be supported in foreign country return HANZI; default: throw new ArgumentException(); } } /// version in question /// /// number of bits used, in this QR Code symbol {@link Version}, to encode the /// count of characters that will follow encoded in this {@link Mode} /// public int getCharacterCountBits(Version version) { if (characterCountBitsForVersions == null) { throw new ArgumentException("Character count doesn't apply to this mode"); } int number = version.VersionNumber; int offset; if (number <= 9) { offset = 0; } else if (number <= 26) { offset = 1; } else { offset = 2; } return characterCountBitsForVersions[offset]; } /// /// Gets the bits. /// public int Bits { get { return bits; } } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { return name; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/QRCodeDecoderMetaData.cs ================================================ /* * Copyright 2013 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.QrCode.Internal { /// /// Meta-data container for QR Code decoding. Instances of this class may be used to convey information back to the /// decoding caller. Callers are expected to process this. /// public sealed class QRCodeDecoderMetaData { private readonly bool mirrored; /// /// Initializes a new instance of the class. /// /// if set to true [mirrored]. public QRCodeDecoderMetaData(bool mirrored) { this.mirrored = mirrored; } /// /// true if the QR Code was mirrored. /// public bool IsMirrored { get { return mirrored; } } /// /// Apply the result points' order correction due to mirroring. /// /// Array of points to apply mirror correction to. public void applyMirroredCorrection(ResultPoint[] points) { if (!mirrored || points == null || points.Length < 3) { return; } ResultPoint bottomLeft = points[0]; points[0] = points[2]; points[2] = bottomLeft; // No need to 'fix' top-left and alignment pattern. } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/decoder/Version.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using ZXing.Common; namespace ZXing.QrCode.Internal { /// /// See ISO 18004:2006 Annex D /// /// Sean Owen public sealed class Version { /// See ISO 18004:2006 Annex D. /// Element i represents the raw version bits that specify version i + 7 /// private static readonly int[] VERSION_DECODE_INFO = new[] { 0x07C94, 0x085BC, 0x09A99, 0x0A4D3, 0x0BBF6, 0x0C762, 0x0D847, 0x0E60D, 0x0F928, 0x10B78, 0x1145D, 0x12A17, 0x13532, 0x149A6, 0x15683, 0x168C9, 0x177EC, 0x18EC4, 0x191E1, 0x1AFAB, 0x1B08E, 0x1CC1A, 0x1D33F, 0x1ED75, 0x1F250, 0x209D5, 0x216F0, 0x228BA, 0x2379F, 0x24B0B, 0x2542E, 0x26A64, 0x27541, 0x28C69 }; private static readonly Version[] VERSIONS = buildVersions(); private readonly int versionNumber; private readonly int[] alignmentPatternCenters; private readonly ECBlocks[] ecBlocks; private readonly int totalCodewords; private Version(int versionNumber, int[] alignmentPatternCenters, params ECBlocks[] ecBlocks) { this.versionNumber = versionNumber; this.alignmentPatternCenters = alignmentPatternCenters; this.ecBlocks = ecBlocks; int total = 0; int ecCodewords = ecBlocks[0].ECCodewordsPerBlock; ECB[] ecbArray = ecBlocks[0].getECBlocks(); foreach (var ecBlock in ecbArray) { total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords); } this.totalCodewords = total; } /// /// Gets the version number. /// public int VersionNumber { get { return versionNumber; } } /// /// Gets the alignment pattern centers. /// public int[] AlignmentPatternCenters { get { return alignmentPatternCenters; } } /// /// Gets the total codewords. /// public int TotalCodewords { get { return totalCodewords; } } /// /// Gets the dimension for version. /// public int DimensionForVersion { get { return 17 + 4 * versionNumber; } } /// /// Gets the EC blocks for level. /// /// The ec level. /// public ECBlocks getECBlocksForLevel(ErrorCorrectionLevel ecLevel) { return ecBlocks[ecLevel.ordinal()]; } ///

Deduces version information purely from QR Code dimensions.

/// ///
/// dimension in modules /// /// for a QR Code of that dimension or null public static Version getProvisionalVersionForDimension(int dimension) { if (dimension % 4 != 1) { return null; } try { return getVersionForNumber((dimension - 17) >> 2); } catch (ArgumentException) { return null; } } /// /// Gets the version for number. /// /// The version number. /// public static Version getVersionForNumber(int versionNumber) { if (versionNumber < 1 || versionNumber > 40) { throw new ArgumentException(); } return VERSIONS[versionNumber - 1]; } internal static Version decodeVersionInformation(int versionBits) { int bestDifference = Int32.MaxValue; int bestVersion = 0; for (int i = 0; i < VERSION_DECODE_INFO.Length; i++) { int targetVersion = VERSION_DECODE_INFO[i]; // Do the version info bits match exactly? done. if (targetVersion == versionBits) { return getVersionForNumber(i + 7); } // Otherwise see if this is the closest to a real version info bit string // we have seen so far int bitsDifference = FormatInformation.numBitsDiffering(versionBits, targetVersion); if (bitsDifference < bestDifference) { bestVersion = i + 7; bestDifference = bitsDifference; } } // We can tolerate up to 3 bits of error since no two version info codewords will // differ in less than 8 bits. if (bestDifference <= 3) { return getVersionForNumber(bestVersion); } // If we didn't find a close enough match, fail return null; } /// See ISO 18004:2006 Annex E internal BitMatrix buildFunctionPattern() { int dimension = DimensionForVersion; BitMatrix bitMatrix = new BitMatrix(dimension); // Top left finder pattern + separator + format bitMatrix.setRegion(0, 0, 9, 9); // Top right finder pattern + separator + format bitMatrix.setRegion(dimension - 8, 0, 8, 9); // Bottom left finder pattern + separator + format bitMatrix.setRegion(0, dimension - 8, 9, 8); // Alignment patterns int max = alignmentPatternCenters.Length; for (int x = 0; x < max; x++) { int i = alignmentPatternCenters[x] - 2; for (int y = 0; y < max; y++) { if ((x == 0 && (y == 0 || y == max - 1)) || (x == max - 1 && y == 0)) { // No alignment patterns near the three finder paterns continue; } bitMatrix.setRegion(alignmentPatternCenters[y] - 2, i, 5, 5); } } // Vertical timing pattern bitMatrix.setRegion(6, 9, 1, dimension - 17); // Horizontal timing pattern bitMatrix.setRegion(9, 6, dimension - 17, 1); if (versionNumber > 6) { // Version info, top right bitMatrix.setRegion(dimension - 11, 0, 3, 6); // Version info, bottom left bitMatrix.setRegion(0, dimension - 11, 6, 3); } return bitMatrix; } ///

Encapsulates a set of error-correction blocks in one symbol version. Most versions will /// use blocks of differing sizes within one version, so, this encapsulates the parameters for /// each set of blocks. It also holds the number of error-correction codewords per block since it /// will be the same across all blocks within one version.

///
public sealed class ECBlocks { private readonly int ecCodewordsPerBlock; private readonly ECB[] ecBlocks; internal ECBlocks(int ecCodewordsPerBlock, params ECB[] ecBlocks) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = ecBlocks; } /// /// Gets the EC codewords per block. /// public int ECCodewordsPerBlock { get { return ecCodewordsPerBlock; } } /// /// Gets the num blocks. /// public int NumBlocks { get { int total = 0; foreach (var ecBlock in ecBlocks) { total += ecBlock.Count; } return total; } } /// /// Gets the total EC codewords. /// public int TotalECCodewords { get { return ecCodewordsPerBlock * NumBlocks; } } /// /// Gets the EC blocks. /// /// public ECB[] getECBlocks() { return ecBlocks; } } ///

Encapsualtes the parameters for one error-correction block in one symbol version. /// This includes the number of data codewords, and the number of times a block with these /// parameters is used consecutively in the QR code version's format.

///
public sealed class ECB { private readonly int count; private readonly int dataCodewords; internal ECB(int count, int dataCodewords) { this.count = count; this.dataCodewords = dataCodewords; } /// /// Gets the count. /// public int Count { get { return count; } } /// /// Gets the data codewords. /// public int DataCodewords { get { return dataCodewords; } } } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { return Convert.ToString(versionNumber); } /// See ISO 18004:2006 6.5.1 Table 9 private static Version[] buildVersions() { return new Version[]{ new Version(1, new int[]{}, new ECBlocks(7, new ECB(1, 19)), new ECBlocks(10, new ECB(1, 16)), new ECBlocks(13, new ECB(1, 13)), new ECBlocks(17, new ECB(1, 9))), new Version(2, new int[]{6, 18}, new ECBlocks(10, new ECB(1, 34)), new ECBlocks(16, new ECB(1, 28)), new ECBlocks(22, new ECB(1, 22)), new ECBlocks(28, new ECB(1, 16))), new Version(3, new int[]{6, 22}, new ECBlocks(15, new ECB(1, 55)), new ECBlocks(26, new ECB(1, 44)), new ECBlocks(18, new ECB(2, 17)), new ECBlocks(22, new ECB(2, 13))), new Version(4, new int[]{6, 26}, new ECBlocks(20, new ECB(1, 80)), new ECBlocks(18, new ECB(2, 32)), new ECBlocks(26, new ECB(2, 24)), new ECBlocks(16, new ECB(4, 9))), new Version(5, new int[]{6, 30}, new ECBlocks(26, new ECB(1, 108)), new ECBlocks(24, new ECB(2, 43)), new ECBlocks(18, new ECB(2, 15), new ECB(2, 16)), new ECBlocks(22, new ECB(2, 11), new ECB(2, 12))), new Version(6, new int[]{6, 34}, new ECBlocks(18, new ECB(2, 68)), new ECBlocks(16, new ECB(4, 27)), new ECBlocks(24, new ECB(4, 19)), new ECBlocks(28, new ECB(4, 15))), new Version(7, new int[]{6, 22, 38}, new ECBlocks(20, new ECB(2, 78)), new ECBlocks(18, new ECB(4, 31)), new ECBlocks(18, new ECB(2, 14), new ECB(4, 15)), new ECBlocks(26, new ECB(4, 13), new ECB(1, 14))), new Version(8, new int[]{6, 24, 42}, new ECBlocks(24, new ECB(2, 97)), new ECBlocks(22, new ECB(2, 38), new ECB(2, 39)), new ECBlocks(22, new ECB(4, 18), new ECB(2, 19)), new ECBlocks(26, new ECB(4, 14), new ECB(2, 15))), new Version(9, new int[]{6, 26, 46}, new ECBlocks(30, new ECB(2, 116)), new ECBlocks(22, new ECB(3, 36), new ECB(2, 37)), new ECBlocks(20, new ECB(4, 16), new ECB(4, 17)), new ECBlocks(24, new ECB(4, 12), new ECB(4, 13))), new Version(10, new int[]{6, 28, 50}, new ECBlocks(18, new ECB(2, 68), new ECB(2, 69)), new ECBlocks(26, new ECB(4, 43), new ECB(1, 44)), new ECBlocks(24, new ECB(6, 19), new ECB(2, 20)), new ECBlocks(28, new ECB(6, 15), new ECB(2, 16))), new Version(11, new int[]{6, 30, 54}, new ECBlocks(20, new ECB(4, 81)), new ECBlocks(30, new ECB(1, 50), new ECB(4, 51)), new ECBlocks(28, new ECB(4, 22), new ECB(4, 23)), new ECBlocks(24, new ECB(3, 12), new ECB(8, 13))), new Version(12, new int[]{6, 32, 58}, new ECBlocks(24, new ECB(2, 92), new ECB(2, 93)), new ECBlocks(22, new ECB(6, 36), new ECB(2, 37)), new ECBlocks(26, new ECB(4, 20), new ECB(6, 21)), new ECBlocks(28, new ECB(7, 14), new ECB(4, 15))), new Version(13, new int[]{6, 34, 62}, new ECBlocks(26, new ECB(4, 107)), new ECBlocks(22, new ECB(8, 37), new ECB(1, 38)), new ECBlocks(24, new ECB(8, 20), new ECB(4, 21)), new ECBlocks(22, new ECB(12, 11), new ECB(4, 12))), new Version(14, new int[]{6, 26, 46, 66}, new ECBlocks(30, new ECB(3, 115), new ECB(1, 116)), new ECBlocks(24, new ECB(4, 40), new ECB(5, 41)), new ECBlocks(20, new ECB(11, 16), new ECB(5, 17)), new ECBlocks(24, new ECB(11, 12), new ECB(5, 13))), new Version(15, new int[]{6, 26, 48, 70}, new ECBlocks(22, new ECB(5, 87), new ECB(1, 88)), new ECBlocks(24, new ECB(5, 41), new ECB(5, 42)), new ECBlocks(30, new ECB(5, 24), new ECB(7, 25)), new ECBlocks(24, new ECB(11, 12), new ECB(7, 13))), new Version(16, new int[]{6, 26, 50, 74}, new ECBlocks(24, new ECB(5, 98), new ECB(1, 99)), new ECBlocks(28, new ECB(7, 45), new ECB(3, 46)), new ECBlocks(24, new ECB(15, 19), new ECB(2, 20)), new ECBlocks(30, new ECB(3, 15), new ECB(13, 16))), new Version(17, new int[]{6, 30, 54, 78}, new ECBlocks(28, new ECB(1, 107), new ECB(5, 108)), new ECBlocks(28, new ECB(10, 46), new ECB(1, 47)), new ECBlocks(28, new ECB(1, 22), new ECB(15, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(17, 15))), new Version(18, new int[]{6, 30, 56, 82}, new ECBlocks(30, new ECB(5, 120), new ECB(1, 121)), new ECBlocks(26, new ECB(9, 43), new ECB(4, 44)), new ECBlocks(28, new ECB(17, 22), new ECB(1, 23)), new ECBlocks(28, new ECB(2, 14), new ECB(19, 15))), new Version(19, new int[]{6, 30, 58, 86}, new ECBlocks(28, new ECB(3, 113), new ECB(4, 114)), new ECBlocks(26, new ECB(3, 44), new ECB(11, 45)), new ECBlocks(26, new ECB(17, 21), new ECB(4, 22)), new ECBlocks(26, new ECB(9, 13), new ECB(16, 14))), new Version(20, new int[]{6, 34, 62, 90}, new ECBlocks(28, new ECB(3, 107), new ECB(5, 108)), new ECBlocks(26, new ECB(3, 41), new ECB(13, 42)), new ECBlocks(30, new ECB(15, 24), new ECB(5, 25)), new ECBlocks(28, new ECB(15, 15), new ECB(10, 16))), new Version(21, new int[]{6, 28, 50, 72, 94}, new ECBlocks(28, new ECB(4, 116), new ECB(4, 117)), new ECBlocks(26, new ECB(17, 42)), new ECBlocks(28, new ECB(17, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(19, 16), new ECB(6, 17))), new Version(22, new int[]{6, 26, 50, 74, 98}, new ECBlocks(28, new ECB(2, 111), new ECB(7, 112)), new ECBlocks(28, new ECB(17, 46)), new ECBlocks(30, new ECB(7, 24), new ECB(16, 25)), new ECBlocks(24, new ECB(34, 13))), new Version(23, new int[]{6, 30, 54, 74, 102}, new ECBlocks(30, new ECB(4, 121), new ECB(5, 122)), new ECBlocks(28, new ECB(4, 47), new ECB(14, 48)), new ECBlocks(30, new ECB(11, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(16, 15), new ECB(14, 16))), new Version(24, new int[]{6, 28, 54, 80, 106}, new ECBlocks(30, new ECB(6, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(6, 45), new ECB(14, 46)), new ECBlocks(30, new ECB(11, 24), new ECB(16, 25)), new ECBlocks(30, new ECB(30, 16), new ECB(2, 17))), new Version(25, new int[]{6, 32, 58, 84, 110}, new ECBlocks(26, new ECB(8, 106), new ECB(4, 107)), new ECBlocks(28, new ECB(8, 47), new ECB(13, 48)), new ECBlocks(30, new ECB(7, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(13, 16))), new Version(26, new int[]{6, 30, 58, 86, 114}, new ECBlocks(28, new ECB(10, 114), new ECB(2, 115)), new ECBlocks(28, new ECB(19, 46), new ECB(4, 47)), new ECBlocks(28, new ECB(28, 22), new ECB(6, 23)), new ECBlocks(30, new ECB(33, 16), new ECB(4, 17))), new Version(27, new int[]{6, 34, 62, 90, 118}, new ECBlocks(30, new ECB(8, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(22, 45), new ECB(3, 46)), new ECBlocks(30, new ECB(8, 23), new ECB(26, 24)), new ECBlocks(30, new ECB(12, 15), new ECB(28, 16))), new Version(28, new int[]{6, 26, 50, 74, 98, 122}, new ECBlocks(30, new ECB(3, 117), new ECB(10, 118)), new ECBlocks(28, new ECB(3, 45), new ECB(23, 46)), new ECBlocks(30, new ECB(4, 24), new ECB(31, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(31, 16))), new Version(29, new int[]{6, 30, 54, 78, 102, 126}, new ECBlocks(30, new ECB(7, 116), new ECB(7, 117)), new ECBlocks(28, new ECB(21, 45), new ECB(7, 46)), new ECBlocks(30, new ECB(1, 23), new ECB(37, 24)), new ECBlocks(30, new ECB(19, 15), new ECB(26, 16))), new Version(30, new int[]{6, 26, 52, 78, 104, 130}, new ECBlocks(30, new ECB(5, 115), new ECB(10, 116)), new ECBlocks(28, new ECB(19, 47), new ECB(10, 48)), new ECBlocks(30, new ECB(15, 24), new ECB(25, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(25, 16))), new Version(31, new int[]{6, 30, 56, 82, 108, 134}, new ECBlocks(30, new ECB(13, 115), new ECB(3, 116)), new ECBlocks(28, new ECB(2, 46), new ECB(29, 47)), new ECBlocks(30, new ECB(42, 24), new ECB(1, 25)), new ECBlocks(30, new ECB(23, 15), new ECB(28, 16))), new Version(32, new int[]{6, 34, 60, 86, 112, 138}, new ECBlocks(30, new ECB(17, 115)), new ECBlocks(28, new ECB(10, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(10, 24), new ECB(35, 25)), new ECBlocks(30, new ECB(19, 15), new ECB(35, 16))), new Version(33, new int[]{6, 30, 58, 86, 114, 142}, new ECBlocks(30, new ECB(17, 115), new ECB(1, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(21, 47)), new ECBlocks(30, new ECB(29, 24), new ECB(19, 25)), new ECBlocks(30, new ECB(11, 15), new ECB(46, 16))), new Version(34, new int[]{6, 34, 62, 90, 118, 146}, new ECBlocks(30, new ECB(13, 115), new ECB(6, 116)), new ECBlocks(28, new ECB(14, 46), new ECB(23, 47)), new ECBlocks(30, new ECB(44, 24), new ECB(7, 25)), new ECBlocks(30, new ECB(59, 16), new ECB(1, 17))), new Version(35, new int[]{6, 30, 54, 78, 102, 126, 150}, new ECBlocks(30, new ECB(12, 121), new ECB(7, 122)), new ECBlocks(28, new ECB(12, 47), new ECB(26, 48)), new ECBlocks(30, new ECB(39, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(22, 15), new ECB(41, 16))), new Version(36, new int[]{6, 24, 50, 76, 102, 128, 154}, new ECBlocks(30, new ECB(6, 121), new ECB(14, 122)), new ECBlocks(28, new ECB(6, 47), new ECB(34, 48)), new ECBlocks(30, new ECB(46, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(2, 15), new ECB(64, 16))), new Version(37, new int[]{6, 28, 54, 80, 106, 132, 158}, new ECBlocks(30, new ECB(17, 122), new ECB(4, 123)), new ECBlocks(28, new ECB(29, 46), new ECB(14, 47)), new ECBlocks(30, new ECB(49, 24), new ECB(10, 25)), new ECBlocks(30, new ECB(24, 15), new ECB(46, 16))), new Version(38, new int[]{6, 32, 58, 84, 110, 136, 162}, new ECBlocks(30, new ECB(4, 122), new ECB(18, 123)), new ECBlocks(28, new ECB(13, 46), new ECB(32, 47)), new ECBlocks(30, new ECB(48, 24), new ECB(14, 25)), new ECBlocks(30, new ECB(42, 15), new ECB(32, 16))), new Version(39, new int[]{6, 26, 54, 82, 110, 138, 166}, new ECBlocks(30, new ECB(20, 117), new ECB(4, 118)), new ECBlocks(28, new ECB(40, 47), new ECB(7, 48)), new ECBlocks(30, new ECB(43, 24), new ECB(22, 25)), new ECBlocks(30, new ECB(10, 15), new ECB(67, 16))), new Version(40, new int[]{6, 30, 58, 86, 114, 142, 170}, new ECBlocks(30, new ECB(19, 118), new ECB(6, 119)), new ECBlocks(28, new ECB(18, 47), new ECB(31, 48)), new ECBlocks(30, new ECB(34, 24), new ECB(34, 25)), new ECBlocks(30, new ECB(20, 15), new ECB(61, 16)))}; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPattern.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { ///

Encapsulates an alignment pattern, which are the smaller square patterns found in /// all but the simplest QR Codes.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// public sealed class AlignmentPattern : ResultPoint { private float estimatedModuleSize; internal AlignmentPattern(float posX, float posY, float estimatedModuleSize) : base(posX, posY) { this.estimatedModuleSize = estimatedModuleSize; } ///

Determines if this alignment pattern "about equals" an alignment pattern at the stated /// position and size -- meaning, it is at nearly the same center with nearly the same size.

///
internal bool aboutEquals(float moduleSize, float i, float j) { if (Math.Abs(i - Y) <= moduleSize && Math.Abs(j - X) <= moduleSize) { float moduleSizeDiff = Math.Abs(moduleSize - estimatedModuleSize); return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize; } return false; } /// /// Combines this object's current estimate of a finder pattern position and module size /// with a new estimate. It returns a new {@code FinderPattern} containing an average of the two. /// /// The i. /// The j. /// New size of the module. /// internal AlignmentPattern combineEstimate(float i, float j, float newModuleSize) { float combinedX = (X + j) / 2.0f; float combinedY = (Y + i) / 2.0f; float combinedModuleSize = (estimatedModuleSize + newModuleSize) / 2.0f; return new AlignmentPattern(combinedX, combinedY, combinedModuleSize); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/AlignmentPatternFinder.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using ZXing.Common; namespace ZXing.QrCode.Internal { ///

This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder /// patterns but are smaller and appear at regular intervals throughout the image.

/// ///

At the moment this only looks for the bottom-right alignment pattern.

/// ///

This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied, /// pasted and stripped down here for maximum performance but does unfortunately duplicate /// some code.

/// ///

This class is thread-safe but not reentrant. Each thread must allocate its own object.

/// ///
/// Sean Owen /// /// www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source /// sealed class AlignmentPatternFinder { private readonly BitMatrix image; private readonly IList possibleCenters; private readonly int startX; private readonly int startY; private readonly int width; private readonly int height; private readonly float moduleSize; private readonly int[] crossCheckStateCount; private readonly ResultPointCallback resultPointCallback; ///

Creates a finder that will look in a portion of the whole image.

/// ///
/// image to search /// /// left column from which to start searching /// /// top row from which to start searching /// /// width of region to search /// /// height of region to search /// /// estimated module size so far /// internal AlignmentPatternFinder(BitMatrix image, int startX, int startY, int width, int height, float moduleSize, ResultPointCallback resultPointCallback) { this.image = image; this.possibleCenters = new List(5); this.startX = startX; this.startY = startY; this.width = width; this.height = height; this.moduleSize = moduleSize; this.crossCheckStateCount = new int[3]; this.resultPointCallback = resultPointCallback; } ///

This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since /// it's pretty performance-critical and so is written to be fast foremost.

/// ///
/// {@link AlignmentPattern} if found /// internal AlignmentPattern find() { int startX = this.startX; int height = this.height; int maxJ = startX + width; int middleI = startY + (height >> 1); // We are looking for black/white/black modules in 1:1:1 ratio; // this tracks the number of black/white/black modules seen so far int[] stateCount = new int[3]; for (int iGen = 0; iGen < height; iGen++) { // Search from middle outwards int i = middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1)); stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; int j = startX; // Burn off leading white pixels before anything else; if we start in the middle of // a white run, it doesn't make sense to count its length, since we don't know if the // white run continued to the left of the start point while (j < maxJ && !image[j, i]) { j++; } int currentState = 0; while (j < maxJ) { if (image[j, i]) { // Black pixel if (currentState == 1) { // Counting black pixels stateCount[currentState]++; } else { // Counting white pixels if (currentState == 2) { // A winner? if (foundPatternCross(stateCount)) { // Yes AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, j); if (confirmed != null) { return confirmed; } } stateCount[0] = stateCount[2]; stateCount[1] = 1; stateCount[2] = 0; currentState = 1; } else { stateCount[++currentState]++; } } } else { // White pixel if (currentState == 1) { // Counting black pixels currentState++; } stateCount[currentState]++; } j++; } if (foundPatternCross(stateCount)) { AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, maxJ); if (confirmed != null) { return confirmed; } } } // Hmm, nothing we saw was observed and confirmed twice. If we had // any guess at all, return it. if (possibleCenters.Count != 0) { return possibleCenters[0]; } return null; } /// Given a count of black/white/black pixels just seen and an end position, /// figures the location of the center of this black/white/black run. /// private static float? centerFromEnd(int[] stateCount, int end) { var result = (end - stateCount[2]) - stateCount[1] / 2.0f; if (Single.IsNaN(result)) return null; return result; } /// count of black/white/black pixels just read /// /// true iff the proportions of the counts is close enough to the 1/1/1 ratios /// used by alignment patterns to be considered a match /// private bool foundPatternCross(int[] stateCount) { float maxVariance = moduleSize / 2.0f; for (int i = 0; i < 3; i++) { if (Math.Abs(moduleSize - stateCount[i]) >= maxVariance) { return false; } } return true; } /// ///

After a horizontal scan finds a potential alignment pattern, this method /// "cross-checks" by scanning down vertically through the center of the possible /// alignment pattern to see if the same proportion is detected.

///
/// row where an alignment pattern was detected /// center of the section that appears to cross an alignment pattern /// maximum reasonable number of modules that should be /// observed in any reading state, based on the results of the horizontal scan /// The original state count total. /// /// vertical center of alignment pattern, or null if not found /// private float? crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) { int maxI = image.Height; int[] stateCount = crossCheckStateCount; stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; // Start counting up from center int i = startI; while (i >= 0 && image[centerJ, i] && stateCount[1] <= maxCount) { stateCount[1]++; i--; } // If already too many modules in this state or ran off the edge: if (i < 0 || stateCount[1] > maxCount) { return null; } while (i >= 0 && !image[centerJ, i] && stateCount[0] <= maxCount) { stateCount[0]++; i--; } if (stateCount[0] > maxCount) { return null; } // Now also count down from center i = startI + 1; while (i < maxI && image[centerJ, i] && stateCount[1] <= maxCount) { stateCount[1]++; i++; } if (i == maxI || stateCount[1] > maxCount) { return null; } while (i < maxI && !image[centerJ, i] && stateCount[2] <= maxCount) { stateCount[2]++; i++; } if (stateCount[2] > maxCount) { return null; } int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) { return null; } return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : null; } ///

This is called when a horizontal scan finds a possible alignment pattern. It will /// cross check with a vertical scan, and if successful, will see if this pattern had been /// found on a previous horizontal scan. If so, we consider it confirmed and conclude we have /// found the alignment pattern.

/// ///
/// reading state module counts from horizontal scan /// /// row where alignment pattern may be found /// /// end of possible alignment pattern in row /// /// {@link AlignmentPattern} if we have found the same pattern twice, or null if not /// private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j) { int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2]; float? centerJ = centerFromEnd(stateCount, j); if (centerJ == null) return null; float? centerI = crossCheckVertical(i, (int)centerJ, 2 * stateCount[1], stateCountTotal); if (centerI != null) { float estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f; foreach (var center in possibleCenters) { // Look for about the same center and module size: if (center.aboutEquals(estimatedModuleSize, centerI.Value, centerJ.Value)) { return center.combineEstimate(centerI.Value, centerJ.Value, estimatedModuleSize); } } // Hadn't found this before; save it var point = new AlignmentPattern(centerJ.Value, centerI.Value, estimatedModuleSize); possibleCenters.Add(point); if (resultPointCallback != null) { resultPointCallback(point); } } return null; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/Detector.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using ZXing.Common; using ZXing.Common.Detector; namespace ZXing.QrCode.Internal { /// ///

Encapsulates logic that can detect a QR Code in an image, even if the QR Code /// is rotated or skewed, or partially obscured.

///
/// Sean Owen public class Detector { private readonly BitMatrix image; private ResultPointCallback resultPointCallback; /// /// Initializes a new instance of the class. /// /// The image. public Detector(BitMatrix image) { this.image = image; } /// /// Gets the image. /// virtual protected internal BitMatrix Image { get { return image; } } /// /// Gets the result point callback. /// virtual protected internal ResultPointCallback ResultPointCallback { get { return resultPointCallback; } } /// ///

Detects a QR Code in an image, simply.

///
/// /// encapsulating results of detecting a QR Code /// public virtual DetectorResult detect() { return detect(null); } /// ///

Detects a QR Code in an image, simply.

///
/// optional hints to detector /// /// encapsulating results of detecting a QR Code /// public virtual DetectorResult detect(IDictionary hints) { resultPointCallback = hints == null || !hints.ContainsKey(DecodeHintType.NEED_RESULT_POINT_CALLBACK) ? null : (ResultPointCallback)hints[DecodeHintType.NEED_RESULT_POINT_CALLBACK]; FinderPatternFinder finder = new FinderPatternFinder(image, resultPointCallback); FinderPatternInfo info = finder.find(hints); if (info == null) return null; return processFinderPatternInfo(info); } /// /// Processes the finder pattern info. /// /// The info. /// protected internal virtual DetectorResult processFinderPatternInfo(FinderPatternInfo info) { FinderPattern topLeft = info.TopLeft; FinderPattern topRight = info.TopRight; FinderPattern bottomLeft = info.BottomLeft; float moduleSize = calculateModuleSize(topLeft, topRight, bottomLeft); if (moduleSize < 1.0f) { return null; } int dimension; if (!computeDimension(topLeft, topRight, bottomLeft, moduleSize, out dimension)) return null; Internal.Version provisionalVersion = Internal.Version.getProvisionalVersionForDimension(dimension); if (provisionalVersion == null) return null; int modulesBetweenFPCenters = provisionalVersion.DimensionForVersion - 7; AlignmentPattern alignmentPattern = null; // Anything above version 1 has an alignment pattern if (provisionalVersion.AlignmentPatternCenters.Length > 0) { // Guess where a "bottom right" finder pattern would have been float bottomRightX = topRight.X - topLeft.X + bottomLeft.X; float bottomRightY = topRight.Y - topLeft.Y + bottomLeft.Y; // Estimate that alignment pattern is closer by 3 modules // from "bottom right" to known top left location //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" float correctionToTopLeft = 1.0f - 3.0f / (float)modulesBetweenFPCenters; //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int estAlignmentX = (int)(topLeft.X + correctionToTopLeft * (bottomRightX - topLeft.X)); //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int estAlignmentY = (int)(topLeft.Y + correctionToTopLeft * (bottomRightY - topLeft.Y)); // Kind of arbitrary -- expand search radius before giving up for (int i = 4; i <= 16; i <<= 1) { alignmentPattern = findAlignmentInRegion(moduleSize, estAlignmentX, estAlignmentY, (float)i); if (alignmentPattern == null) continue; break; } // If we didn't find alignment pattern... well try anyway without it } PerspectiveTransform transform = createTransform(topLeft, topRight, bottomLeft, alignmentPattern, dimension); BitMatrix bits = sampleGrid(image, transform, dimension); if (bits == null) return null; ResultPoint[] points; if (alignmentPattern == null) { points = new ResultPoint[] { bottomLeft, topLeft, topRight }; } else { points = new ResultPoint[] { bottomLeft, topLeft, topRight, alignmentPattern }; } return new DetectorResult(bits, points); } private static PerspectiveTransform createTransform(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, ResultPoint alignmentPattern, int dimension) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" float dimMinusThree = (float)dimension - 3.5f; float bottomRightX; float bottomRightY; float sourceBottomRightX; float sourceBottomRightY; if (alignmentPattern != null) { bottomRightX = alignmentPattern.X; bottomRightY = alignmentPattern.Y; sourceBottomRightX = sourceBottomRightY = dimMinusThree - 3.0f; } else { // Don't have an alignment pattern, just make up the bottom-right point bottomRightX = (topRight.X - topLeft.X) + bottomLeft.X; bottomRightY = (topRight.Y - topLeft.Y) + bottomLeft.Y; sourceBottomRightX = sourceBottomRightY = dimMinusThree; } return PerspectiveTransform.quadrilateralToQuadrilateral( 3.5f, 3.5f, dimMinusThree, 3.5f, sourceBottomRightX, sourceBottomRightY, 3.5f, dimMinusThree, topLeft.X, topLeft.Y, topRight.X, topRight.Y, bottomRightX, bottomRightY, bottomLeft.X, bottomLeft.Y); } private static BitMatrix sampleGrid(BitMatrix image, PerspectiveTransform transform, int dimension) { GridSampler sampler = GridSampler.Instance; return sampler.sampleGrid(image, dimension, dimension, transform); } ///

Computes the dimension (number of modules on a size) of the QR Code based on the position /// of the finder patterns and estimated module size.

///
private static bool computeDimension(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft, float moduleSize, out int dimension) { int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize); int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize); dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7; switch (dimension & 0x03) { // mod 4 case 0: dimension++; break; // 1? do nothing case 2: dimension--; break; case 3: return true; } return true; } ///

Computes an average estimated module size based on estimated derived from the positions /// of the three finder patterns.

///
protected internal virtual float calculateModuleSize(ResultPoint topLeft, ResultPoint topRight, ResultPoint bottomLeft) { // Take the average return (calculateModuleSizeOneWay(topLeft, topRight) + calculateModuleSizeOneWay(topLeft, bottomLeft)) / 2.0f; } ///

Estimates module size based on two finder patterns -- it uses /// {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the /// width of each, measuring along the axis between their centers.

///
private float calculateModuleSizeOneWay(ResultPoint pattern, ResultPoint otherPattern) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" float moduleSizeEst1 = sizeOfBlackWhiteBlackRunBothWays((int)pattern.X, (int)pattern.Y, (int)otherPattern.X, (int)otherPattern.Y); //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" float moduleSizeEst2 = sizeOfBlackWhiteBlackRunBothWays((int)otherPattern.X, (int)otherPattern.Y, (int)pattern.X, (int)pattern.Y); if (Single.IsNaN(moduleSizeEst1)) { return moduleSizeEst2 / 7.0f; } if (Single.IsNaN(moduleSizeEst2)) { return moduleSizeEst1 / 7.0f; } // Average them, and divide by 7 since we've counted the width of 3 black modules, // and 1 white and 1 black module on either side. Ergo, divide sum by 14. return (moduleSizeEst1 + moduleSizeEst2) / 14.0f; } /// See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of /// a finder pattern by looking for a black-white-black run from the center in the direction /// of another point (another finder pattern center), and in the opposite direction too. /// private float sizeOfBlackWhiteBlackRunBothWays(int fromX, int fromY, int toX, int toY) { float result = sizeOfBlackWhiteBlackRun(fromX, fromY, toX, toY); // Now count other way -- don't run off image though of course float scale = 1.0f; int otherToX = fromX - (toX - fromX); if (otherToX < 0) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" scale = (float)fromX / (float)(fromX - otherToX); otherToX = 0; } else if (otherToX >= image.Width) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" scale = (float)(image.Width - 1 - fromX) / (float)(otherToX - fromX); otherToX = image.Width - 1; } //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int otherToY = (int)(fromY - (toY - fromY) * scale); scale = 1.0f; if (otherToY < 0) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" scale = (float)fromY / (float)(fromY - otherToY); otherToY = 0; } else if (otherToY >= image.Height) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" scale = (float)(image.Height - 1 - fromY) / (float)(otherToY - fromY); otherToY = image.Height - 1; } //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" otherToX = (int)(fromX + (otherToX - fromX) * scale); result += sizeOfBlackWhiteBlackRun(fromX, fromY, otherToX, otherToY); return result - 1.0f; // -1 because we counted the middle pixel twice } ///

This method traces a line from a point in the image, in the direction towards another point. /// It begins in a black region, and keeps going until it finds white, then black, then white again. /// It reports the distance from the start to this point.

/// ///

This is used when figuring out how wide a finder pattern is, when the finder pattern /// may be skewed or rotated.

///
private float sizeOfBlackWhiteBlackRun(int fromX, int fromY, int toX, int toY) { // Mild variant of Bresenham's algorithm; // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm bool steep = Math.Abs(toY - fromY) > Math.Abs(toX - fromX); if (steep) { int temp = fromX; fromX = fromY; fromY = temp; temp = toX; toX = toY; toY = temp; } int dx = Math.Abs(toX - fromX); int dy = Math.Abs(toY - fromY); int error = -dx >> 1; int xstep = fromX < toX ? 1 : -1; int ystep = fromY < toY ? 1 : -1; // In black pixels, looking for white, first or second time. int state = 0; // Loop up until x == toX, but not beyond int xLimit = toX + xstep; for (int x = fromX, y = fromY; x != xLimit; x += xstep) { int realX = steep ? y : x; int realY = steep ? x : y; // Does current pixel mean we have moved white to black or vice versa? // Scanning black in state 0,2 and white in state 1, so if we find the wrong // color, advance to next state or end if we are in state 2 already if ((state == 1) == image[realX, realY]) { if (state == 2) { return MathUtils.distance(x, y, fromX, fromY); } state++; } error += dy; if (error > 0) { if (y == toY) { break; } y += ystep; error -= dx; } } // Found black-white-black; give the benefit of the doubt that the next pixel outside the image // is "white" so this last point at (toX+xStep,toY) is the right ending. This is really a // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this. if (state == 2) { return MathUtils.distance(toX + xstep, toY, fromX, fromY); } // else we didn't find even black-white-black; no estimate is really possible return Single.NaN; } /// ///

Attempts to locate an alignment pattern in a limited region of the image, which is /// guessed to contain it. This method uses {@link AlignmentPattern}.

///
/// estimated module size so far /// x coordinate of center of area probably containing alignment pattern /// y coordinate of above /// number of pixels in all directions to search from the center /// /// if found, or null otherwise /// protected AlignmentPattern findAlignmentInRegion(float overallEstModuleSize, int estAlignmentX, int estAlignmentY, float allowanceFactor) { // Look for an alignment pattern (3 modules in size) around where it // should be //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int allowance = (int)(allowanceFactor * overallEstModuleSize); int alignmentAreaLeftX = Math.Max(0, estAlignmentX - allowance); int alignmentAreaRightX = Math.Min(image.Width - 1, estAlignmentX + allowance); if (alignmentAreaRightX - alignmentAreaLeftX < overallEstModuleSize * 3) { return null; } int alignmentAreaTopY = Math.Max(0, estAlignmentY - allowance); int alignmentAreaBottomY = Math.Min(image.Height - 1, estAlignmentY + allowance); var alignmentFinder = new AlignmentPatternFinder( image, alignmentAreaLeftX, alignmentAreaTopY, alignmentAreaRightX - alignmentAreaLeftX, alignmentAreaBottomY - alignmentAreaTopY, overallEstModuleSize, resultPointCallback); return alignmentFinder.find(); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPattern.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { /// ///

Encapsulates a finder pattern, which are the three square patterns found in /// the corners of QR Codes. It also encapsulates a count of similar finder patterns, /// as a convenience to the finder's bookkeeping.

///
/// Sean Owen public sealed class FinderPattern : ResultPoint { private readonly float estimatedModuleSize; private int count; internal FinderPattern(float posX, float posY, float estimatedModuleSize) : this(posX, posY, estimatedModuleSize, 1) { this.estimatedModuleSize = estimatedModuleSize; this.count = 1; } internal FinderPattern(float posX, float posY, float estimatedModuleSize, int count) : base(posX, posY) { this.estimatedModuleSize = estimatedModuleSize; this.count = count; } /// /// Gets the size of the estimated module. /// /// /// The size of the estimated module. /// public float EstimatedModuleSize { get { return estimatedModuleSize; } } internal int Count { get { return count; } } /* internal void incrementCount() { this.count++; } */ ///

Determines if this finder pattern "about equals" a finder pattern at the stated /// position and size -- meaning, it is at nearly the same center with nearly the same size.

///
internal bool aboutEquals(float moduleSize, float i, float j) { if (Math.Abs(i - Y) <= moduleSize && Math.Abs(j - X) <= moduleSize) { float moduleSizeDiff = Math.Abs(moduleSize - estimatedModuleSize); return moduleSizeDiff <= 1.0f || moduleSizeDiff <= estimatedModuleSize; } return false; } /// /// Combines this object's current estimate of a finder pattern position and module size /// with a new estimate. It returns a new {@code FinderPattern} containing a weighted average /// based on count. /// /// The i. /// The j. /// New size of the module. /// internal FinderPattern combineEstimate(float i, float j, float newModuleSize) { int combinedCount = count + 1; float combinedX = (count * X + j) / combinedCount; float combinedY = (count * Y + i) / combinedCount; float combinedModuleSize = (count * estimatedModuleSize + newModuleSize) / combinedCount; return new FinderPattern(combinedX, combinedY, combinedModuleSize, combinedCount); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternFinder.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using ZXing.Common; namespace ZXing.QrCode.Internal { /// ///

This class attempts to find finder patterns in a QR Code. Finder patterns are the square /// markers at three corners of a QR Code.

/// ///

This class is thread-safe but not reentrant. Each thread must allocate its own object. ///

/// Sean Owen public class FinderPatternFinder { private const int CENTER_QUORUM = 2; /// /// 1 pixel/module times 3 modules/center /// protected internal const int MIN_SKIP = 3; /// /// support up to version 10 for mobile clients /// protected internal const int MAX_MODULES = 57; private const int INTEGER_MATH_SHIFT = 8; private readonly BitMatrix image; private List possibleCenters; private bool hasSkipped; private readonly int[] crossCheckStateCount; private readonly ResultPointCallback resultPointCallback; /// ///

Creates a finder that will search the image for three finder patterns.

///
/// image to search public FinderPatternFinder(BitMatrix image) : this(image, null) { } /// /// Initializes a new instance of the class. /// /// The image. /// The result point callback. public FinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback) { this.image = image; this.possibleCenters = new List(); this.crossCheckStateCount = new int[5]; this.resultPointCallback = resultPointCallback; } /// /// Gets the image. /// virtual protected internal BitMatrix Image { get { return image; } } /// /// Gets the possible centers. /// virtual protected internal List PossibleCenters { get { return possibleCenters; } } internal virtual FinderPatternInfo find(IDictionary hints) { bool tryHarder = hints != null && hints.ContainsKey(DecodeHintType.TRY_HARDER); bool pureBarcode = hints != null && hints.ContainsKey(DecodeHintType.PURE_BARCODE); int maxI = image.Height; int maxJ = image.Width; // We are looking for black/white/black/white/black modules in // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the // image, and then account for the center being 3 modules in size. This gives the smallest // number of pixels the center could be, so skip this often. When trying harder, look for all // QR versions regardless of how dense they are. int iSkip = (3 * maxI) / (4 * MAX_MODULES); if (iSkip < MIN_SKIP || tryHarder) { iSkip = MIN_SKIP; } bool done = false; int[] stateCount = new int[5]; for (int i = iSkip - 1; i < maxI && !done; i += iSkip) { // Get a row of black/white values stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; stateCount[3] = 0; stateCount[4] = 0; int currentState = 0; for (int j = 0; j < maxJ; j++) { if (image[j, i]) { // Black pixel if ((currentState & 1) == 1) { // Counting white pixels currentState++; } stateCount[currentState]++; } else { // White pixel if ((currentState & 1) == 0) { // Counting black pixels if (currentState == 4) { // A winner? if (foundPatternCross(stateCount)) { // Yes bool confirmed = handlePossibleCenter(stateCount, i, j, pureBarcode); if (confirmed) { // Start examining every other line. Checking each line turned out to be too // expensive and didn't improve performance. iSkip = 2; if (hasSkipped) { done = haveMultiplyConfirmedCenters(); } else { int rowSkip = findRowSkip(); if (rowSkip > stateCount[2]) { // Skip rows between row of lower confirmed center // and top of presumed third confirmed center // but back up a bit to get a full chance of detecting // it, entire width of center of finder pattern // Skip by rowSkip, but back off by stateCount[2] (size of last center // of pattern we saw) to be conservative, and also back off by iSkip which // is about to be re-added i += rowSkip - stateCount[2] - iSkip; j = maxJ - 1; } } } else { stateCount[0] = stateCount[2]; stateCount[1] = stateCount[3]; stateCount[2] = stateCount[4]; stateCount[3] = 1; stateCount[4] = 0; currentState = 3; continue; } // Clear state to start looking again currentState = 0; stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; stateCount[3] = 0; stateCount[4] = 0; } else { // No, shift counts back by two stateCount[0] = stateCount[2]; stateCount[1] = stateCount[3]; stateCount[2] = stateCount[4]; stateCount[3] = 1; stateCount[4] = 0; currentState = 3; } } else { stateCount[++currentState]++; } } else { // Counting white pixels stateCount[currentState]++; } } } if (foundPatternCross(stateCount)) { bool confirmed = handlePossibleCenter(stateCount, i, maxJ, pureBarcode); if (confirmed) { iSkip = stateCount[0]; if (hasSkipped) { // Found a third one done = haveMultiplyConfirmedCenters(); } } } } FinderPattern[] patternInfo = selectBestPatterns(); if (patternInfo == null) return null; ResultPoint.orderBestPatterns(patternInfo); return new FinderPatternInfo(patternInfo); } /// Given a count of black/white/black/white/black pixels just seen and an end position, /// figures the location of the center of this run. /// private static float? centerFromEnd(int[] stateCount, int end) { var result = (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0f; if (Single.IsNaN(result)) return null; return result; } /// count of black/white/black/white/black pixels just read /// /// true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios /// used by finder patterns to be considered a match /// protected internal static bool foundPatternCross(int[] stateCount) { int totalModuleSize = 0; for (int i = 0; i < 5; i++) { int count = stateCount[i]; if (count == 0) { return false; } totalModuleSize += count; } if (totalModuleSize < 7) { return false; } int moduleSize = (totalModuleSize << INTEGER_MATH_SHIFT) / 7; int maxVariance = moduleSize / 2; // Allow less than 50% variance from 1-1-3-1-1 proportions return Math.Abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) < maxVariance && Math.Abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) < maxVariance && Math.Abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) < 3 * maxVariance && Math.Abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) < maxVariance && Math.Abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance; } private int[] CrossCheckStateCount { get { crossCheckStateCount[0] = 0; crossCheckStateCount[1] = 0; crossCheckStateCount[2] = 0; crossCheckStateCount[3] = 0; crossCheckStateCount[4] = 0; return crossCheckStateCount; } } /// /// After a vertical and horizontal scan finds a potential finder pattern, this method /// "cross-cross-cross-checks" by scanning down diagonally through the center of the possible /// finder pattern to see if the same proportion is detected. /// /// row where a finder pattern was detected /// center of the section that appears to cross a finder pattern /// maximum reasonable number of modules that should be observed in any reading state, based on the results of the horizontal scan /// The original state count total. /// true if proportions are withing expected limits private bool crossCheckDiagonal(int startI, int centerJ, int maxCount, int originalStateCountTotal) { int maxI = image.Height; int maxJ = image.Width; int[] stateCount = CrossCheckStateCount; // Start counting up, left from center finding black center mass int i = 0; while (startI - i >= 0 && image[centerJ - i, startI - i]) { stateCount[2]++; i++; } if ((startI - i < 0) || (centerJ - i < 0)) { return false; } // Continue up, left finding white space while ((startI - i >= 0) && (centerJ - i >= 0) && !image[centerJ - i, startI - i] && stateCount[1] <= maxCount) { stateCount[1]++; i++; } // If already too many modules in this state or ran off the edge: if ((startI - i < 0) || (centerJ - i < 0) || stateCount[1] > maxCount) { return false; } // Continue up, left finding black border while ((startI - i >= 0) && (centerJ - i >= 0) && image[centerJ - i, startI - i] && stateCount[0] <= maxCount) { stateCount[0]++; i++; } if (stateCount[0] > maxCount) { return false; } // Now also count down, right from center i = 1; while ((startI + i < maxI) && (centerJ + i < maxJ) && image[centerJ + i, startI + i]) { stateCount[2]++; i++; } // Ran off the edge? if ((startI + i >= maxI) || (centerJ + i >= maxJ)) { return false; } while ((startI + i < maxI) && (centerJ + i < maxJ) && !image[centerJ + i, startI + i] && stateCount[3] < maxCount) { stateCount[3]++; i++; } if ((startI + i >= maxI) || (centerJ + i >= maxJ) || stateCount[3] >= maxCount) { return false; } while ((startI + i < maxI) && (centerJ + i < maxJ) && image[centerJ + i, startI + i] && stateCount[4] < maxCount) { stateCount[4]++; i++; } if (stateCount[4] >= maxCount) { return false; } // If we found a finder-pattern-like section, but its size is more than 100% different than // the original, assume it's a false positive int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; return Math.Abs(stateCountTotal - originalStateCountTotal) < 2*originalStateCountTotal && foundPatternCross(stateCount); } /// ///

After a horizontal scan finds a potential finder pattern, this method /// "cross-checks" by scanning down vertically through the center of the possible /// finder pattern to see if the same proportion is detected.

///
/// row where a finder pattern was detected /// center of the section that appears to cross a finder pattern /// maximum reasonable number of modules that should be /// observed in any reading state, based on the results of the horizontal scan /// The original state count total. /// /// vertical center of finder pattern, or null if not found /// private float? crossCheckVertical(int startI, int centerJ, int maxCount, int originalStateCountTotal) { int maxI = image.Height; int[] stateCount = CrossCheckStateCount; // Start counting up from center int i = startI; while (i >= 0 && image[centerJ, i]) { stateCount[2]++; i--; } if (i < 0) { return null; } while (i >= 0 && !image[centerJ, i] && stateCount[1] <= maxCount) { stateCount[1]++; i--; } // If already too many modules in this state or ran off the edge: if (i < 0 || stateCount[1] > maxCount) { return null; } while (i >= 0 && image[centerJ, i] && stateCount[0] <= maxCount) { stateCount[0]++; i--; } if (stateCount[0] > maxCount) { return null; } // Now also count down from center i = startI + 1; while (i < maxI && image[centerJ, i]) { stateCount[2]++; i++; } if (i == maxI) { return null; } while (i < maxI && !image[centerJ, i] && stateCount[3] < maxCount) { stateCount[3]++; i++; } if (i == maxI || stateCount[3] >= maxCount) { return null; } while (i < maxI && image[centerJ, i] && stateCount[4] < maxCount) { stateCount[4]++; i++; } if (stateCount[4] >= maxCount) { return null; } // If we found a finder-pattern-like section, but its size is more than 40% different than // the original, assume it's a false positive int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) { return null; } return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : null; } ///

Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical, /// except it reads horizontally instead of vertically. This is used to cross-cross /// check a vertical cross check and locate the real center of the alignment pattern.

///
private float? crossCheckHorizontal(int startJ, int centerI, int maxCount, int originalStateCountTotal) { int maxJ = image.Width; int[] stateCount = CrossCheckStateCount; int j = startJ; while (j >= 0 && image[j, centerI]) { stateCount[2]++; j--; } if (j < 0) { return null; } while (j >= 0 && !image[j, centerI] && stateCount[1] <= maxCount) { stateCount[1]++; j--; } if (j < 0 || stateCount[1] > maxCount) { return null; } while (j >= 0 && image[j, centerI] && stateCount[0] <= maxCount) { stateCount[0]++; j--; } if (stateCount[0] > maxCount) { return null; } j = startJ + 1; while (j < maxJ && image[j, centerI]) { stateCount[2]++; j++; } if (j == maxJ) { return null; } while (j < maxJ && !image[j, centerI] && stateCount[3] < maxCount) { stateCount[3]++; j++; } if (j == maxJ || stateCount[3] >= maxCount) { return null; } while (j < maxJ && image[j, centerI] && stateCount[4] < maxCount) { stateCount[4]++; j++; } if (stateCount[4] >= maxCount) { return null; } // If we found a finder-pattern-like section, but its size is significantly different than // the original, assume it's a false positive int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; if (5 * Math.Abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) { return null; } return foundPatternCross(stateCount) ? centerFromEnd(stateCount, j) : null; } /// ///

This is called when a horizontal scan finds a possible alignment pattern. It will /// cross check with a vertical scan, and if successful, will, ah, cross-cross-check /// with another horizontal scan. This is needed primarily to locate the real horizontal /// center of the pattern in cases of extreme skew. /// And then we cross-cross-cross check with another diagonal scan.

/// If that succeeds the finder pattern location is added to a list that tracks /// the number of times each location has been nearly-matched as a finder pattern. /// Each additional find is more evidence that the location is in fact a finder /// pattern center ///
/// reading state module counts from horizontal scan /// row where finder pattern may be found /// end of possible finder pattern in row /// if set to true [pure barcode]. /// /// true if a finder pattern candidate was found this time /// protected bool handlePossibleCenter(int[] stateCount, int i, int j, bool pureBarcode) { int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] + stateCount[4]; float? centerJ = centerFromEnd(stateCount, j); if (centerJ == null) return false; float? centerI = crossCheckVertical(i, (int)centerJ.Value, stateCount[2], stateCountTotal); if (centerI != null) { // Re-cross check centerJ = crossCheckHorizontal((int)centerJ.Value, (int)centerI.Value, stateCount[2], stateCountTotal); if (centerJ != null && (!pureBarcode || crossCheckDiagonal((int) centerI, (int) centerJ, stateCount[2], stateCountTotal))) { float estimatedModuleSize = stateCountTotal / 7.0f; bool found = false; for (int index = 0; index < possibleCenters.Count; index++) { var center = possibleCenters[index]; // Look for about the same center and module size: if (center.aboutEquals(estimatedModuleSize, centerI.Value, centerJ.Value)) { possibleCenters.RemoveAt(index); possibleCenters.Insert(index, center.combineEstimate(centerI.Value, centerJ.Value, estimatedModuleSize)); found = true; break; } } if (!found) { var point = new FinderPattern(centerJ.Value, centerI.Value, estimatedModuleSize); possibleCenters.Add(point); if (resultPointCallback != null) { resultPointCallback(point); } } return true; } } return false; } /// number of rows we could safely skip during scanning, based on the first /// two finder patterns that have been located. In some cases their position will /// allow us to infer that the third pattern must lie below a certain point farther /// down in the image. /// private int findRowSkip() { int max = possibleCenters.Count; if (max <= 1) { return 0; } ResultPoint firstConfirmedCenter = null; foreach (var center in possibleCenters) { if (center.Count >= CENTER_QUORUM) { if (firstConfirmedCenter == null) { firstConfirmedCenter = center; } else { // We have two confirmed centers // How far down can we skip before resuming looking for the next // pattern? In the worst case, only the difference between the // difference in the x / y coordinates of the two centers. // This is the case where you find top left last. hasSkipped = true; //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" return (int)(Math.Abs(firstConfirmedCenter.X - center.X) - Math.Abs(firstConfirmedCenter.Y - center.Y)) / 2; } } } return 0; } /// true iff we have found at least 3 finder patterns that have been detected /// at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the /// candidates is "pretty similar" /// private bool haveMultiplyConfirmedCenters() { int confirmedCount = 0; float totalModuleSize = 0.0f; int max = possibleCenters.Count; foreach (var pattern in possibleCenters) { if (pattern.Count >= CENTER_QUORUM) { confirmedCount++; totalModuleSize += pattern.EstimatedModuleSize; } } if (confirmedCount < 3) { return false; } // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive" // and that we need to keep looking. We detect this by asking if the estimated module sizes // vary too much. We arbitrarily say that when the total deviation from average exceeds // 5% of the total module size estimates, it's too much. float average = totalModuleSize / max; float totalDeviation = 0.0f; for (int i = 0; i < max; i++) { var pattern = possibleCenters[i]; totalDeviation += Math.Abs(pattern.EstimatedModuleSize - average); } return totalDeviation <= 0.05f * totalModuleSize; } /// the 3 best {@link FinderPattern}s from our list of candidates. The "best" are /// those that have been detected at least {@link #CENTER_QUORUM} times, and whose module /// size differs from the average among those patterns the least /// private FinderPattern[] selectBestPatterns() { int startSize = possibleCenters.Count; if (startSize < 3) { // Couldn't find enough finder patterns return null; } // Filter outlier possibilities whose module size is too different if (startSize > 3) { // But we can only afford to do so if we have at least 4 possibilities to choose from float totalModuleSize = 0.0f; float square = 0.0f; foreach (var center in possibleCenters) { float size = center.EstimatedModuleSize; totalModuleSize += size; square += size * size; } float average = totalModuleSize / startSize; float stdDev = (float)Math.Sqrt(square / startSize - average * average); possibleCenters.Sort(new FurthestFromAverageComparator(average)); float limit = Math.Max(0.2f * average, stdDev); for (int i = 0; i < possibleCenters.Count && possibleCenters.Count > 3; i++) { FinderPattern pattern = possibleCenters[i]; if (Math.Abs(pattern.EstimatedModuleSize - average) > limit) { possibleCenters.RemoveAt(i); i--; } } } if (possibleCenters.Count > 3) { // Throw away all but those first size candidate points we found. float totalModuleSize = 0.0f; foreach (var possibleCenter in possibleCenters) { totalModuleSize += possibleCenter.EstimatedModuleSize; } float average = totalModuleSize / possibleCenters.Count; possibleCenters.Sort(new CenterComparator(average)); //possibleCenters.subList(3, possibleCenters.Count).clear(); possibleCenters = possibleCenters.GetRange(0, 3); } return new[] { possibleCenters[0], possibleCenters[1], possibleCenters[2] }; } /// /// Orders by furthest from average /// private sealed class FurthestFromAverageComparator : IComparer { private readonly float average; public FurthestFromAverageComparator(float f) { average = f; } public int Compare(FinderPattern x, FinderPattern y) { float dA = Math.Abs(y.EstimatedModuleSize - average); float dB = Math.Abs(x.EstimatedModuleSize - average); return dA < dB ? -1 : dA == dB ? 0 : 1; } } ///

Orders by {@link FinderPattern#getCount()}, descending.

private sealed class CenterComparator : IComparer { private readonly float average; public CenterComparator(float f) { average = f; } public int Compare(FinderPattern x, FinderPattern y) { if (y.Count == x.Count) { float dA = Math.Abs(y.EstimatedModuleSize - average); float dB = Math.Abs(x.EstimatedModuleSize - average); return dA < dB ? 1 : dA == dB ? 0 : -1; } return y.Count - x.Count; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/detector/FinderPatternInfo.cs ================================================ /* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.QrCode.Internal { /// ///

Encapsulates information about finder patterns in an image, including the location of /// the three finder patterns, and their estimated module size.

///
/// Sean Owen public sealed class FinderPatternInfo { private readonly FinderPattern bottomLeft; private readonly FinderPattern topLeft; private readonly FinderPattern topRight; /// /// Initializes a new instance of the class. /// /// The pattern centers. public FinderPatternInfo(FinderPattern[] patternCenters) { this.bottomLeft = patternCenters[0]; this.topLeft = patternCenters[1]; this.topRight = patternCenters[2]; } /// /// Gets the bottom left. /// public FinderPattern BottomLeft { get { return bottomLeft; } } /// /// Gets the top left. /// public FinderPattern TopLeft { get { return topLeft; } } /// /// Gets the top right. /// public FinderPattern TopRight { get { return topRight; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/BlockPair.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ namespace ZXing.QrCode.Internal { internal sealed class BlockPair { private readonly byte[] dataBytes; private readonly byte[] errorCorrectionBytes; public BlockPair(byte[] data, byte[] errorCorrection) { dataBytes = data; errorCorrectionBytes = errorCorrection; } public byte[] DataBytes { get { return dataBytes; } } public byte[] ErrorCorrectionBytes { get { return errorCorrectionBytes; } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/ByteMatrix.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Text; namespace ZXing.QrCode.Internal { /// /// JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned /// 0, 1 and 2 I'm going to use less memory and go with bytes. /// /// dswitkin@google.com (Daniel Switkin) public sealed class ByteMatrix { private readonly byte[][] bytes; private readonly int width; private readonly int height; /// /// Initializes a new instance of the class. /// /// The width. /// The height. public ByteMatrix(int width, int height) { bytes = new byte[height][]; for (var i = 0; i < height; i++) bytes[i] = new byte[width]; this.width = width; this.height = height; } /// /// Gets the height. /// public int Height { get { return height; } } /// /// Gets the width. /// public int Width { get { return width; } } /// /// Gets or sets the with the specified x. /// public int this[int x, int y] { get { return bytes[y][x]; } set { bytes[y][x] = (byte)value; } } /// /// an internal representation as bytes, in row-major order. array[y][x] represents point (x,y) /// public byte[][] Array { get { return bytes; } } /// /// Sets the specified x. /// /// The x. /// The y. /// The value. public void set(int x, int y, byte value) { bytes[y][x] = value; } /// /// Sets the specified x. /// /// The x. /// The y. /// if set to true [value]. public void set(int x, int y, bool value) { bytes[y][x] = (byte)(value ? 1 : 0); } /// /// Clears the specified value. /// /// The value. public void clear(byte value) { for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { bytes[y][x] = value; } } } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// override public String ToString() { var result = new StringBuilder(2 * width * height + 2); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { switch (bytes[y][x]) { case 0: result.Append(" 0"); break; case 1: result.Append(" 1"); break; default: result.Append(" "); break; } } result.Append('\n'); } return result.ToString(); } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/Encoder.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Collections.Generic; using System.Text; using ZXing.Common; using ZXing.Common.ReedSolomon; namespace ZXing.QrCode.Internal { /// /// /// satorux@google.com (Satoru Takabayashi) - creator /// dswitkin@google.com (Daniel Switkin) - ported from C++ public static class Encoder { // The original table is defined in the table 5 of JISX0510:2004 (p.19). private static readonly int[] ALPHANUMERIC_TABLE = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00-0x0f -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10-0x1f 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, // 0x20-0x2f 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, // 0x30-0x3f -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 0x40-0x4f 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, // 0x50-0x5f }; internal static String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1"; // The mask penalty calculation is complicated. See Table 21 of JISX0510:2004 (p.45) for details. // Basically it applies four rules and summate all penalties. private static int calculateMaskPenalty(ByteMatrix matrix) { return MaskUtil.applyMaskPenaltyRule1(matrix) + MaskUtil.applyMaskPenaltyRule2(matrix) + MaskUtil.applyMaskPenaltyRule3(matrix) + MaskUtil.applyMaskPenaltyRule4(matrix); } /// /// Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen /// internally by chooseMode(). On success, store the result in "qrCode". /// We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for /// "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very /// strong error correction for this purpose. /// Note that there is no way to encode bytes in MODE_KANJI. We might want to add EncodeWithMode() /// with which clients can specify the encoding mode. For now, we don't need the functionality. /// /// The content. /// The ec level. public static QRCode encode(String content, ErrorCorrectionLevel ecLevel) { return encode(content, ecLevel, null); } /// /// Encodes the specified content. /// /// The content. /// The ec level. /// The hints. /// public static QRCode encode(String content, ErrorCorrectionLevel ecLevel, IDictionary hints) { // Determine what character encoding has been specified by the caller, if any #if !SILVERLIGHT || WINDOWS_PHONE String encoding = hints == null || !hints.ContainsKey(EncodeHintType.CHARACTER_SET) ? null : (String)hints[EncodeHintType.CHARACTER_SET]; if (encoding == null) { encoding = DEFAULT_BYTE_MODE_ENCODING; } bool generateECI = !DEFAULT_BYTE_MODE_ENCODING.Equals(encoding); #else // Silverlight supports only UTF-8 and UTF-16 out-of-the-box const string encoding = "UTF-8"; // caller of the method can only control if the ECI segment should be written // character set is fixed to UTF-8; but some scanners doesn't like the ECI segment bool generateECI = (hints != null && hints.ContainsKey(EncodeHintType.CHARACTER_SET)); #endif // Pick an encoding mode appropriate for the content. Note that this will not attempt to use // multiple modes / segments even if that were more efficient. Twould be nice. Mode mode = chooseMode(content, encoding); // This will store the header information, like mode and // length, as well as "header" segments like an ECI segment. BitArray headerBits = new BitArray(); // (With ECI in place,) Write the mode marker appendModeInfo(mode, headerBits); // Collect data within the main segment, separately, to count its size if needed. Don't add it to // main payload yet. BitArray dataBits = new BitArray(); appendBytes(content, mode, dataBits, encoding); // Hard part: need to know version to know how many bits length takes. But need to know how many // bits it takes to know version. First we take a guess at version by assuming version will be // the minimum, 1: int provisionalBitsNeeded = headerBits.Size + mode.getCharacterCountBits(Version.getVersionForNumber(1)) + dataBits.Size; Version provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel); // Use that guess to calculate the right version. I am still not sure this works in 100% of cases. int bitsNeeded = headerBits.Size + mode.getCharacterCountBits(provisionalVersion) + dataBits.Size; Version version = chooseVersion(bitsNeeded, ecLevel); BitArray headerAndDataBits = new BitArray(); headerAndDataBits.appendBitArray(headerBits); // Find "length" of main segment and write it int numLetters = mode == Mode.BYTE ? dataBits.SizeInBytes : content.Length; appendLengthInfo(numLetters, version, mode, headerAndDataBits); // Put data together into the overall payload headerAndDataBits.appendBitArray(dataBits); Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); int numDataBytes = version.TotalCodewords - ecBlocks.TotalECCodewords; // Terminate the bits properly. terminateBits(numDataBytes, headerAndDataBits); // Interleave data bits with error correction code. BitArray finalBits = interleaveWithECBytes(headerAndDataBits, version.TotalCodewords, numDataBytes, ecBlocks.NumBlocks); QRCode qrCode = new QRCode { ECLevel = ecLevel, Mode = mode, Version = version }; // Choose the mask pattern and set to "qrCode". int dimension = version.DimensionForVersion; ByteMatrix matrix = new ByteMatrix(dimension, dimension); int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix); qrCode.MaskPattern = maskPattern; // Build the matrix and set it to "qrCode". MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix); qrCode.Matrix = matrix; return qrCode; } /// /// Gets the alphanumeric code. /// /// The code. /// the code point of the table used in alphanumeric mode or /// -1 if there is no corresponding code in the table. internal static int getAlphanumericCode(int code) { if (code < ALPHANUMERIC_TABLE.Length) { return ALPHANUMERIC_TABLE[code]; } return -1; } /// /// Chooses the mode. /// /// The content. /// public static Mode chooseMode(String content) { return chooseMode(content, null); } /// /// Choose the best mode by examining the content. Note that 'encoding' is used as a hint; /// if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. /// /// The content. /// The encoding. /// private static Mode chooseMode(String content, String encoding) { return Mode.BYTE; } private static int chooseMaskPattern(BitArray bits, ErrorCorrectionLevel ecLevel, Version version, ByteMatrix matrix) { int minPenalty = Int32.MaxValue; // Lower penalty is better. int bestMaskPattern = -1; // We try all mask patterns to choose the best one. for (int maskPattern = 0; maskPattern < QRCode.NUM_MASK_PATTERNS; maskPattern++) { MatrixUtil.buildMatrix(bits, ecLevel, version, maskPattern, matrix); int penalty = calculateMaskPenalty(matrix); if (penalty < minPenalty) { minPenalty = penalty; bestMaskPattern = maskPattern; } } return bestMaskPattern; } private static Version chooseVersion(int numInputBits, ErrorCorrectionLevel ecLevel) { // In the following comments, we use numbers of Version 7-H. for (int versionNum = 1; versionNum <= 40; versionNum++) { Version version = Version.getVersionForNumber(versionNum); // numBytes = 196 int numBytes = version.TotalCodewords; // getNumECBytes = 130 Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel); int numEcBytes = ecBlocks.TotalECCodewords; // getNumDataBytes = 196 - 130 = 66 int numDataBytes = numBytes - numEcBytes; int totalInputBytes = (numInputBits + 7) / 8; if (numDataBytes >= totalInputBytes) { return version; } } throw new WriterException("Data too big"); } /// /// Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24). /// /// The num data bytes. /// The bits. internal static void terminateBits(int numDataBytes, BitArray bits) { int capacity = numDataBytes << 3; if (bits.Size > capacity) { throw new WriterException("data bits cannot fit in the QR Code" + bits.Size + " > " + capacity); } for (int i = 0; i < 4 && bits.Size < capacity; ++i) { bits.appendBit(false); } // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details. // If the last byte isn't 8-bit aligned, we'll add padding bits. int numBitsInLastByte = bits.Size & 0x07; if (numBitsInLastByte > 0) { for (int i = numBitsInLastByte; i < 8; i++) { bits.appendBit(false); } } // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24). int numPaddingBytes = numDataBytes - bits.SizeInBytes; for (int i = 0; i < numPaddingBytes; ++i) { bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8); } if (bits.Size != capacity) { throw new WriterException("Bits size does not equal capacity"); } } /// /// Get number of data bytes and number of error correction bytes for block id "blockID". Store /// the result in "numDataBytesInBlock", and "numECBytesInBlock". See table 12 in 8.5.1 of /// JISX0510:2004 (p.30) /// /// The num total bytes. /// The num data bytes. /// The num RS blocks. /// The block ID. /// The num data bytes in block. /// The num EC bytes in block. internal static void getNumDataBytesAndNumECBytesForBlockID(int numTotalBytes, int numDataBytes, int numRSBlocks, int blockID, int[] numDataBytesInBlock, int[] numECBytesInBlock) { if (blockID >= numRSBlocks) { throw new WriterException("Block ID too large"); } // numRsBlocksInGroup2 = 196 % 5 = 1 int numRsBlocksInGroup2 = numTotalBytes % numRSBlocks; // numRsBlocksInGroup1 = 5 - 1 = 4 int numRsBlocksInGroup1 = numRSBlocks - numRsBlocksInGroup2; // numTotalBytesInGroup1 = 196 / 5 = 39 int numTotalBytesInGroup1 = numTotalBytes / numRSBlocks; // numTotalBytesInGroup2 = 39 + 1 = 40 int numTotalBytesInGroup2 = numTotalBytesInGroup1 + 1; // numDataBytesInGroup1 = 66 / 5 = 13 int numDataBytesInGroup1 = numDataBytes / numRSBlocks; // numDataBytesInGroup2 = 13 + 1 = 14 int numDataBytesInGroup2 = numDataBytesInGroup1 + 1; // numEcBytesInGroup1 = 39 - 13 = 26 int numEcBytesInGroup1 = numTotalBytesInGroup1 - numDataBytesInGroup1; // numEcBytesInGroup2 = 40 - 14 = 26 int numEcBytesInGroup2 = numTotalBytesInGroup2 - numDataBytesInGroup2; // Sanity checks. // 26 = 26 if (numEcBytesInGroup1 != numEcBytesInGroup2) { throw new WriterException("EC bytes mismatch"); } // 5 = 4 + 1. if (numRSBlocks != numRsBlocksInGroup1 + numRsBlocksInGroup2) { throw new WriterException("RS blocks mismatch"); } // 196 = (13 + 26) * 4 + (14 + 26) * 1 if (numTotalBytes != ((numDataBytesInGroup1 + numEcBytesInGroup1) * numRsBlocksInGroup1) + ((numDataBytesInGroup2 + numEcBytesInGroup2) * numRsBlocksInGroup2)) { throw new WriterException("Total bytes mismatch"); } if (blockID < numRsBlocksInGroup1) { numDataBytesInBlock[0] = numDataBytesInGroup1; numECBytesInBlock[0] = numEcBytesInGroup1; } else { numDataBytesInBlock[0] = numDataBytesInGroup2; numECBytesInBlock[0] = numEcBytesInGroup2; } } /// /// Interleave "bits" with corresponding error correction bytes. On success, store the result in /// "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details. /// /// The bits. /// The num total bytes. /// The num data bytes. /// The num RS blocks. /// internal static BitArray interleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) { // "bits" must have "getNumDataBytes" bytes of data. if (bits.SizeInBytes != numDataBytes) { throw new WriterException("Number of bits and data bytes does not match"); } // Step 1. Divide data bytes into blocks and generate error correction bytes for them. We'll // store the divided data bytes blocks and error correction bytes blocks into "blocks". int dataBytesOffset = 0; int maxNumDataBytes = 0; int maxNumEcBytes = 0; // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number. var blocks = new List(numRSBlocks); for (int i = 0; i < numRSBlocks; ++i) { int[] numDataBytesInBlock = new int[1]; int[] numEcBytesInBlock = new int[1]; getNumDataBytesAndNumECBytesForBlockID( numTotalBytes, numDataBytes, numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock); int size = numDataBytesInBlock[0]; byte[] dataBytes = new byte[size]; bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size); byte[] ecBytes = generateECBytes(dataBytes, numEcBytesInBlock[0]); blocks.Add(new BlockPair(dataBytes, ecBytes)); maxNumDataBytes = Math.Max(maxNumDataBytes, size); maxNumEcBytes = Math.Max(maxNumEcBytes, ecBytes.Length); dataBytesOffset += numDataBytesInBlock[0]; } if (numDataBytes != dataBytesOffset) { throw new WriterException("Data bytes does not match offset"); } BitArray result = new BitArray(); // First, place data blocks. for (int i = 0; i < maxNumDataBytes; ++i) { foreach (BlockPair block in blocks) { byte[] dataBytes = block.DataBytes; if (i < dataBytes.Length) { result.appendBits(dataBytes[i], 8); } } } // Then, place error correction blocks. for (int i = 0; i < maxNumEcBytes; ++i) { foreach (BlockPair block in blocks) { byte[] ecBytes = block.ErrorCorrectionBytes; if (i < ecBytes.Length) { result.appendBits(ecBytes[i], 8); } } } if (numTotalBytes != result.SizeInBytes) { // Should be same. throw new WriterException("Interleaving error: " + numTotalBytes + " and " + result.SizeInBytes + " differ."); } return result; } internal static byte[] generateECBytes(byte[] dataBytes, int numEcBytesInBlock) { int numDataBytes = dataBytes.Length; int[] toEncode = new int[numDataBytes + numEcBytesInBlock]; for (int i = 0; i < numDataBytes; i++) { toEncode[i] = dataBytes[i] & 0xFF; } new ReedSolomonEncoder(GenericGF.QR_CODE_FIELD_256).encode(toEncode, numEcBytesInBlock); byte[] ecBytes = new byte[numEcBytesInBlock]; for (int i = 0; i < numEcBytesInBlock; i++) { ecBytes[i] = (byte)toEncode[numDataBytes + i]; } return ecBytes; } /// /// Append mode info. On success, store the result in "bits". /// /// The mode. /// The bits. internal static void appendModeInfo(Mode mode, BitArray bits) { bits.appendBits(mode.Bits, 4); } /// /// Append length info. On success, store the result in "bits". /// /// The num letters. /// The version. /// The mode. /// The bits. internal static void appendLengthInfo(int numLetters, Version version, Mode mode, BitArray bits) { int numBits = mode.getCharacterCountBits(version); if (numLetters >= (1 << numBits)) { throw new WriterException(numLetters + " is bigger than " + ((1 << numBits) - 1)); } bits.appendBits(numLetters, numBits); } /// /// Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits". /// /// The content. /// The mode. /// The bits. /// The encoding. internal static void appendBytes(String content, Mode mode, BitArray bits, String encoding) { if (mode.Equals(Mode.BYTE)) append8BitBytes(content, bits, encoding); else throw new WriterException("Invalid mode: " + mode); } internal static void appendNumericBytes(String content, BitArray bits) { int length = content.Length; int i = 0; while (i < length) { int num1 = content[i] - '0'; if (i + 2 < length) { // Encode three numeric letters in ten bits. int num2 = content[i + 1] - '0'; int num3 = content[i + 2] - '0'; bits.appendBits(num1 * 100 + num2 * 10 + num3, 10); i += 3; } else if (i + 1 < length) { // Encode two numeric letters in seven bits. int num2 = content[i + 1] - '0'; bits.appendBits(num1 * 10 + num2, 7); i += 2; } else { // Encode one numeric letter in four bits. bits.appendBits(num1, 4); i++; } } } internal static void append8BitBytes(String content, BitArray bits, String encoding) { byte[] bytes; try { bytes = Encoding.GetEncoding(encoding).GetBytes(content); } #if WindowsCE catch (PlatformNotSupportedException) { try { // WindowsCE doesn't support all encodings. But it is device depended. // So we try here the some different ones if (encoding == "ISO-8859-1") { bytes = Encoding.GetEncoding(1252).GetBytes(content); } else { bytes = Encoding.GetEncoding("UTF-8").GetBytes(content); } } catch (Exception uee) { throw new WriterException(uee.Message, uee); } } #endif catch (Exception uee) { throw new WriterException(uee.Message, uee); } foreach (byte b in bytes) { bits.appendBits(b, 8); } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/MaskUtil.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; namespace ZXing.QrCode.Internal { /// /// /// /// Satoru Takabayashi /// Daniel Switkin /// Sean Owen public static class MaskUtil { // Penalty weights from section 6.8.2.1 private const int N1 = 3; private const int N2 = 3; private const int N3 = 40; private const int N4 = 10; /// /// Apply mask penalty rule 1 and return the penalty. Find repetitive cells with the same color and /// give penalty to them. Example: 00000 or 11111. /// /// The matrix. /// public static int applyMaskPenaltyRule1(ByteMatrix matrix) { return applyMaskPenaltyRule1Internal(matrix, true) + applyMaskPenaltyRule1Internal(matrix, false); } /// /// Apply mask penalty rule 2 and return the penalty. Find 2x2 blocks with the same color and give /// penalty to them. This is actually equivalent to the spec's rule, which is to find MxN blocks and give a /// penalty proportional to (M-1)x(N-1), because this is the number of 2x2 blocks inside such a block. /// /// The matrix. /// public static int applyMaskPenaltyRule2(ByteMatrix matrix) { int penalty = 0; var array = matrix.Array; int width = matrix.Width; int height = matrix.Height; for (int y = 0; y < height - 1; y++) { for (int x = 0; x < width - 1; x++) { int value = array[y][x]; if (value == array[y][x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1]) { penalty++; } } } return N2 * penalty; } /// /// Apply mask penalty rule 3 and return the penalty. Find consecutive cells of 00001011101 or /// 10111010000, and give penalty to them. If we find patterns like 000010111010000, we give /// penalties twice (i.e. 40 * 2). /// /// The matrix. /// public static int applyMaskPenaltyRule3(ByteMatrix matrix) { int numPenalties = 0; byte[][] array = matrix.Array; int width = matrix.Width; int height = matrix.Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { byte[] arrayY = array[y]; // We can at least optimize this access if (x + 6 < width && arrayY[x] == 1 && arrayY[x + 1] == 0 && arrayY[x + 2] == 1 && arrayY[x + 3] == 1 && arrayY[x + 4] == 1 && arrayY[x + 5] == 0 && arrayY[x + 6] == 1 && (isWhiteHorizontal(arrayY, x - 4, x) || isWhiteHorizontal(arrayY, x + 7, x + 11))) { numPenalties++; } if (y + 6 < height && array[y][x] == 1 && array[y + 1][x] == 0 && array[y + 2][x] == 1 && array[y + 3][x] == 1 && array[y + 4][x] == 1 && array[y + 5][x] == 0 && array[y + 6][x] == 1 && (isWhiteVertical(array, x, y - 4, y) || isWhiteVertical(array, x, y + 7, y + 11))) { numPenalties++; } } } return numPenalties * N3; } private static bool isWhiteHorizontal(byte[] rowArray, int from, int to) { for (int i = from; i < to; i++) { if (i >= 0 && i < rowArray.Length && rowArray[i] == 1) { return false; } } return true; } private static bool isWhiteVertical(byte[][] array, int col, int from, int to) { for (int i = from; i < to; i++) { if (i >= 0 && i < array.Length && array[i][col] == 1) { return false; } } return true; } /// /// Apply mask penalty rule 4 and return the penalty. Calculate the ratio of dark cells and give /// penalty if the ratio is far from 50%. It gives 10 penalty for 5% distance. /// /// The matrix. /// public static int applyMaskPenaltyRule4(ByteMatrix matrix) { int numDarkCells = 0; var array = matrix.Array; int width = matrix.Width; int height = matrix.Height; for (int y = 0; y < height; y++) { var arrayY = array[y]; for (int x = 0; x < width; x++) { if (arrayY[x] == 1) { numDarkCells++; } } } var numTotalCells = matrix.Height * matrix.Width; var darkRatio = (double)numDarkCells / numTotalCells; var fivePercentVariances = (int)(Math.Abs(darkRatio - 0.5) * 20.0); // * 100.0 / 5.0 return fivePercentVariances * N4; } /// /// Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask /// pattern conditions. /// /// The mask pattern. /// The x. /// The y. /// public static bool getDataMaskBit(int maskPattern, int x, int y) { int intermediate, temp; switch (maskPattern) { case 0: intermediate = (y + x) & 0x1; break; case 1: intermediate = y & 0x1; break; case 2: intermediate = x % 3; break; case 3: intermediate = (y + x) % 3; break; case 4: intermediate = (((int)((uint)y >> 1)) + (x / 3)) & 0x1; break; case 5: temp = y * x; intermediate = (temp & 0x1) + (temp % 3); break; case 6: temp = y * x; intermediate = (((temp & 0x1) + (temp % 3)) & 0x1); break; case 7: temp = y * x; intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1); break; default: throw new ArgumentException("Invalid mask pattern: " + maskPattern); } return intermediate == 0; } /// /// Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both /// vertical and horizontal orders respectively. /// /// The matrix. /// if set to true [is horizontal]. /// private static int applyMaskPenaltyRule1Internal(ByteMatrix matrix, bool isHorizontal) { int penalty = 0; int iLimit = isHorizontal ? matrix.Height : matrix.Width; int jLimit = isHorizontal ? matrix.Width : matrix.Height; var array = matrix.Array; for (int i = 0; i < iLimit; i++) { int numSameBitCells = 0; int prevBit = -1; for (int j = 0; j < jLimit; j++) { int bit = isHorizontal ? array[i][j] : array[j][i]; if (bit == prevBit) { numSameBitCells++; } else { if (numSameBitCells >= 5) { penalty += N1 + (numSameBitCells - 5); } numSameBitCells = 1; // Include the cell itself. prevBit = bit; } } if (numSameBitCells >= 5) { penalty += N1 + (numSameBitCells - 5); } } return penalty; } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/MatrixUtil.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using ZXing.Common; namespace ZXing.QrCode.Internal { /// /// /// /// /// satorux@google.com (Satoru Takabayashi) - creator /// public static class MatrixUtil { private static readonly int[][] POSITION_DETECTION_PATTERN = new int[][] { new int[] { 1, 1, 1, 1, 1, 1, 1 }, new int[] { 1, 0, 0, 0, 0, 0, 1 }, new int[] { 1, 0, 1, 1, 1, 0, 1 }, new int[] { 1, 0, 1, 1, 1, 0, 1 }, new int[] { 1, 0, 1, 1, 1, 0, 1 }, new int[] { 1, 0, 0, 0, 0, 0, 1 }, new int[] { 1, 1, 1, 1, 1, 1, 1 } }; private static readonly int[][] POSITION_ADJUSTMENT_PATTERN = new int[][] { new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 0, 0, 0, 1 }, new int[] { 1, 0, 1, 0, 1 }, new int[] { 1, 0, 0, 0, 1 }, new int[] { 1, 1, 1, 1, 1 } }; // From Appendix E. Table 1, JIS0510X:2004 (p 71). The table was double-checked by komatsu. private static readonly int[][] POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE = new int[][] { new int[] { -1, -1, -1, -1, -1, -1, -1 }, new int[] { 6, 18, -1, -1, -1, -1, -1 }, new int[] { 6, 22, -1, -1, -1, -1, -1 }, new int[] { 6, 26, -1, -1, -1, -1, -1 }, new int[] { 6, 30, -1, -1, -1, -1, -1 }, new int[] { 6, 34, -1, -1, -1, -1, -1 }, new int[] { 6, 22, 38, -1, -1, -1, -1 }, new int[] { 6, 24, 42, -1, -1, -1, -1 }, new int[] { 6, 26, 46, -1, -1, -1, -1 }, new int[] { 6, 28, 50, -1, -1, -1, -1 }, new int[] { 6, 30, 54, -1, -1, -1, -1 }, new int[] { 6, 32, 58, -1, -1, -1, -1 }, new int[] { 6, 34, 62, -1, -1, -1, -1 }, new int[] { 6, 26, 46, 66, -1, -1, -1 }, new int[] { 6, 26, 48, 70, -1, -1, -1 }, new int[] { 6, 26, 50, 74, -1, -1, -1 }, new int[] { 6, 30, 54, 78, -1, -1, -1 }, new int[] { 6, 30, 56, 82, -1, -1, -1 }, new int[] { 6, 30, 58, 86, -1, -1, -1 }, new int[] { 6, 34, 62, 90, -1, -1, -1 }, new int[] { 6, 28, 50, 72, 94, -1, -1 }, new int[] { 6, 26, 50, 74, 98, -1, -1 }, new int[] { 6, 30, 54, 78, 102, -1, -1 }, new int[] { 6, 28, 54, 80, 106, -1, -1 }, new int[] { 6, 32, 58, 84, 110, -1, -1 }, new int[] { 6, 30, 58, 86, 114, -1, -1 }, new int[] { 6, 34, 62, 90, 118, -1, -1 }, new int[] { 6, 26, 50, 74, 98, 122, -1 }, new int[] { 6, 30, 54, 78, 102, 126, -1 }, new int[] { 6, 26, 52, 78, 104, 130, -1 }, new int[] { 6, 30, 56, 82, 108, 134, -1 }, new int[] { 6, 34, 60, 86, 112, 138, -1 }, new int[] { 6, 30, 58, 86, 114, 142, -1 }, new int[] { 6, 34, 62, 90, 118, 146, -1 }, new int[] { 6, 30, 54, 78, 102, 126, 150 }, new int[] { 6, 24, 50, 76, 102, 128, 154 }, new int[] { 6, 28, 54, 80, 106, 132, 158 }, new int[] { 6, 32, 58, 84, 110, 136, 162 }, new int[] { 6, 26, 54, 82, 110, 138, 166 }, new int[] { 6, 30, 58, 86, 114, 142, 170 } }; // Type info cells at the left top corner. private static readonly int[][] TYPE_INFO_COORDINATES = new int[][] { new int[] { 8, 0 }, new int[] { 8, 1 }, new int[] { 8, 2 }, new int[] { 8, 3 }, new int[] { 8, 4 }, new int[] { 8, 5 }, new int[] { 8, 7 }, new int[] { 8, 8 }, new int[] { 7, 8 }, new int[] { 5, 8 }, new int[] { 4, 8 }, new int[] { 3, 8 }, new int[] { 2, 8 }, new int[] { 1, 8 }, new int[] { 0, 8 } }; // From Appendix D in JISX0510:2004 (p. 67) private const int VERSION_INFO_POLY = 0x1f25; // 1 1111 0010 0101 // From Appendix C in JISX0510:2004 (p.65). private const int TYPE_INFO_POLY = 0x537; private const int TYPE_INFO_MASK_PATTERN = 0x5412; /// /// Set all cells to 2. 2 means that the cell is empty (not set yet). /// /// JAVAPORT: We shouldn't need to do this at all. The code should be rewritten to begin encoding /// with the ByteMatrix initialized all to zero. /// /// The matrix. public static void clearMatrix(ByteMatrix matrix) { matrix.clear(2); } /// /// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On /// success, store the result in "matrix" and return true. /// /// The data bits. /// The ec level. /// The version. /// The mask pattern. /// The matrix. public static void buildMatrix(BitArray dataBits, ErrorCorrectionLevel ecLevel, Version version, int maskPattern, ByteMatrix matrix) { clearMatrix(matrix); embedBasicPatterns(version, matrix); // Type information appear with any version. embedTypeInfo(ecLevel, maskPattern, matrix); // Version info appear if version >= 7. maybeEmbedVersionInfo(version, matrix); // Data should be embedded at end. embedDataBits(dataBits, maskPattern, matrix); } /// /// Embed basic patterns. On success, modify the matrix and return true. /// The basic patterns are: /// - Position detection patterns /// - Timing patterns /// - Dark dot at the left bottom corner /// - Position adjustment patterns, if need be /// /// The version. /// The matrix. public static void embedBasicPatterns(Version version, ByteMatrix matrix) { // Let's get started with embedding big squares at corners. embedPositionDetectionPatternsAndSeparators(matrix); // Then, embed the dark dot at the left bottom corner. embedDarkDotAtLeftBottomCorner(matrix); // Position adjustment patterns appear if version >= 2. maybeEmbedPositionAdjustmentPatterns(version, matrix); // Timing patterns should be embedded after position adj. patterns. embedTimingPatterns(matrix); } /// /// Embed type information. On success, modify the matrix. /// /// The ec level. /// The mask pattern. /// The matrix. public static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix) { BitArray typeInfoBits = new BitArray(); makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits); for (int i = 0; i < typeInfoBits.Size; ++i) { // Place bits in LSB to MSB order. LSB (least significant bit) is the last value in // "typeInfoBits". int bit = typeInfoBits[typeInfoBits.Size - 1 - i] ? 1 : 0; // Type info bits at the left top corner. See 8.9 of JISX0510:2004 (p.46). int x1 = TYPE_INFO_COORDINATES[i][0]; int y1 = TYPE_INFO_COORDINATES[i][1]; matrix[x1, y1] = bit; if (i < 8) { // Right top corner. int x2 = matrix.Width - i - 1; int y2 = 8; matrix[x2, y2] = bit; } else { // Left bottom corner. int x2 = 8; int y2 = matrix.Height - 7 + (i - 8); matrix[x2, y2] = bit; } } } /// /// Embed version information if need be. On success, modify the matrix and return true. /// See 8.10 of JISX0510:2004 (p.47) for how to embed version information. /// /// The version. /// The matrix. public static void maybeEmbedVersionInfo(Version version, ByteMatrix matrix) { if (version.VersionNumber < 7) { // Version info is necessary if version >= 7. return; // Don't need version info. } BitArray versionInfoBits = new BitArray(); makeVersionInfoBits(version, versionInfoBits); int bitIndex = 6 * 3 - 1; // It will decrease from 17 to 0. for (int i = 0; i < 6; ++i) { for (int j = 0; j < 3; ++j) { // Place bits in LSB (least significant bit) to MSB order. var bit = versionInfoBits[bitIndex] ? 1 : 0; bitIndex--; // Left bottom corner. matrix[i, matrix.Height - 11 + j] = bit; // Right bottom corner. matrix[matrix.Height - 11 + j, i] = bit; } } } /// /// Embed "dataBits" using "getMaskPattern". On success, modify the matrix and return true. /// For debugging purposes, it skips masking process if "getMaskPattern" is -1. /// See 8.7 of JISX0510:2004 (p.38) for how to embed data bits. /// /// The data bits. /// The mask pattern. /// The matrix. public static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix) { int bitIndex = 0; int direction = -1; // Start from the right bottom cell. int x = matrix.Width - 1; int y = matrix.Height - 1; while (x > 0) { // Skip the vertical timing pattern. if (x == 6) { x -= 1; } while (y >= 0 && y < matrix.Height) { for (int i = 0; i < 2; ++i) { int xx = x - i; // Skip the cell if it's not empty. if (!isEmpty(matrix[xx, y])) { continue; } int bit; if (bitIndex < dataBits.Size) { bit = dataBits[bitIndex] ? 1 : 0; ++bitIndex; } else { // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described // in 8.4.9 of JISX0510:2004 (p. 24). bit = 0; } // Skip masking if mask_pattern is -1. if (maskPattern != -1) { if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) { bit ^= 0x1; } } matrix[xx, y] = bit; } y += direction; } direction = -direction; // Reverse the direction. y += direction; x -= 2; // Move to the left. } // All bits should be consumed. if (bitIndex != dataBits.Size) { throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.Size); } } /// /// Return the position of the most significant bit set (to one) in the "value". The most /// significant bit is position 32. If there is no bit set, return 0. Examples: /// - findMSBSet(0) => 0 /// - findMSBSet(1) => 1 /// - findMSBSet(255) => 8 /// /// The value_ renamed. /// public static int findMSBSet(int value_Renamed) { int numDigits = 0; while (value_Renamed != 0) { value_Renamed = (int)((uint)value_Renamed >> 1); ++numDigits; } return numDigits; } /// /// Calculate BCH (Bose-Chaudhuri-Hocquenghem) code for "value" using polynomial "poly". The BCH /// code is used for encoding type information and version information. /// Example: Calculation of version information of 7. /// f(x) is created from 7. /// - 7 = 000111 in 6 bits /// - f(x) = x^2 + x^2 + x^1 /// g(x) is given by the standard (p. 67) /// - g(x) = x^12 + x^11 + x^10 + x^9 + x^8 + x^5 + x^2 + 1 /// Multiply f(x) by x^(18 - 6) /// - f'(x) = f(x) * x^(18 - 6) /// - f'(x) = x^14 + x^13 + x^12 /// Calculate the remainder of f'(x) / g(x) /// x^2 /// __________________________________________________ /// g(x) )x^14 + x^13 + x^12 /// x^14 + x^13 + x^12 + x^11 + x^10 + x^7 + x^4 + x^2 /// -------------------------------------------------- /// x^11 + x^10 + x^7 + x^4 + x^2 /// /// The remainder is x^11 + x^10 + x^7 + x^4 + x^2 /// Encode it in binary: 110010010100 /// The return value is 0xc94 (1100 1001 0100) /// /// Since all coefficients in the polynomials are 1 or 0, we can do the calculation by bit /// operations. We don't care if cofficients are positive or negative. /// /// The value. /// The poly. /// public static int calculateBCHCode(int value, int poly) { // If poly is "1 1111 0010 0101" (version info poly), msbSetInPoly is 13. We'll subtract 1 // from 13 to make it 12. int msbSetInPoly = findMSBSet(poly); value <<= msbSetInPoly - 1; // Do the division business using exclusive-or operations. while (findMSBSet(value) >= msbSetInPoly) { value ^= poly << (findMSBSet(value) - msbSetInPoly); } // Now the "value" is the remainder (i.e. the BCH code) return value; } /// /// Make bit vector of type information. On success, store the result in "bits" and return true. /// Encode error correction level and mask pattern. See 8.9 of /// JISX0510:2004 (p.45) for details. /// /// The ec level. /// The mask pattern. /// The bits. public static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits) { if (!QRCode.isValidMaskPattern(maskPattern)) { throw new WriterException("Invalid mask pattern"); } int typeInfo = (ecLevel.Bits << 3) | maskPattern; bits.appendBits(typeInfo, 5); int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY); bits.appendBits(bchCode, 10); BitArray maskBits = new BitArray(); maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15); bits.xor(maskBits); if (bits.Size != 15) { // Just in case. throw new WriterException("should not happen but we got: " + bits.Size); } } /// /// Make bit vector of version information. On success, store the result in "bits" and return true. /// See 8.10 of JISX0510:2004 (p.45) for details. /// /// The version. /// The bits. public static void makeVersionInfoBits(Version version, BitArray bits) { bits.appendBits(version.VersionNumber, 6); int bchCode = calculateBCHCode(version.VersionNumber, VERSION_INFO_POLY); bits.appendBits(bchCode, 12); if (bits.Size != 18) { // Just in case. throw new WriterException("should not happen but we got: " + bits.Size); } } /// /// Check if "value" is empty. /// /// The value. /// /// true if the specified value is empty; otherwise, false. /// private static bool isEmpty(int value) { return value == 2; } private static void embedTimingPatterns(ByteMatrix matrix) { // -8 is for skipping position detection patterns (size 7), and two horizontal/vertical // separation patterns (size 1). Thus, 8 = 7 + 1. for (int i = 8; i < matrix.Width - 8; ++i) { int bit = (i + 1) % 2; // Horizontal line. if (isEmpty(matrix[i, 6])) { matrix[i, 6] = bit; } // Vertical line. if (isEmpty(matrix[6, i])) { matrix[6, i] = bit; } } } /// /// Embed the lonely dark dot at left bottom corner. JISX0510:2004 (p.46) /// /// The matrix. private static void embedDarkDotAtLeftBottomCorner(ByteMatrix matrix) { if (matrix[8, matrix.Height - 8] == 0) { throw new WriterException(); } matrix[8, matrix.Height - 8] = 1; } private static void embedHorizontalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) { for (int x = 0; x < 8; ++x) { if (!isEmpty(matrix[xStart + x, yStart])) { throw new WriterException(); } matrix[xStart + x, yStart] = 0; } } private static void embedVerticalSeparationPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 7; ++y) { if (!isEmpty(matrix[xStart, yStart + y])) { throw new WriterException(); } matrix[xStart, yStart + y] = 0; } } /// /// Note that we cannot unify the function with embedPositionDetectionPattern() despite they are /// almost identical, since we cannot write a function that takes 2D arrays in different sizes in /// C/C++. We should live with the fact. /// /// The x start. /// The y start. /// The matrix. private static void embedPositionAdjustmentPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 5; ++y) { for (int x = 0; x < 5; ++x) { matrix[xStart + x, yStart + y] = POSITION_ADJUSTMENT_PATTERN[y][x]; } } } private static void embedPositionDetectionPattern(int xStart, int yStart, ByteMatrix matrix) { for (int y = 0; y < 7; ++y) { for (int x = 0; x < 7; ++x) { matrix[xStart + x, yStart + y] = POSITION_DETECTION_PATTERN[y][x]; } } } /// /// Embed position detection patterns and surrounding vertical/horizontal separators. /// /// The matrix. private static void embedPositionDetectionPatternsAndSeparators(ByteMatrix matrix) { // Embed three big squares at corners. int pdpWidth = POSITION_DETECTION_PATTERN[0].Length; // Left top corner. embedPositionDetectionPattern(0, 0, matrix); // Right top corner. embedPositionDetectionPattern(matrix.Width - pdpWidth, 0, matrix); // Left bottom corner. embedPositionDetectionPattern(0, matrix.Width - pdpWidth, matrix); // Embed horizontal separation patterns around the squares. const int hspWidth = 8; // Left top corner. embedHorizontalSeparationPattern(0, hspWidth - 1, matrix); // Right top corner. embedHorizontalSeparationPattern(matrix.Width - hspWidth, hspWidth - 1, matrix); // Left bottom corner. embedHorizontalSeparationPattern(0, matrix.Width - hspWidth, matrix); // Embed vertical separation patterns around the squares. const int vspSize = 7; // Left top corner. embedVerticalSeparationPattern(vspSize, 0, matrix); // Right top corner. embedVerticalSeparationPattern(matrix.Height - vspSize - 1, 0, matrix); // Left bottom corner. embedVerticalSeparationPattern(vspSize, matrix.Height - vspSize, matrix); } /// /// Embed position adjustment patterns if need be. /// /// The version. /// The matrix. private static void maybeEmbedPositionAdjustmentPatterns(Version version, ByteMatrix matrix) { if (version.VersionNumber < 2) { // The patterns appear if version >= 2 return; } int index = version.VersionNumber - 1; int[] coordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index]; int numCoordinates = POSITION_ADJUSTMENT_PATTERN_COORDINATE_TABLE[index].Length; for (int i = 0; i < numCoordinates; ++i) { for (int j = 0; j < numCoordinates; ++j) { int y = coordinates[i]; int x = coordinates[j]; if (x == -1 || y == -1) { continue; } // If the cell is unset, we embed the position adjustment pattern here. if (isEmpty(matrix[x, y])) { // -2 is necessary since the x/y coordinates point to the center of the pattern, not the // left top corner. embedPositionAdjustmentPattern(x - 2, y - 2, matrix); } } } } } } ================================================ FILE: shadowsocks-csharp/3rd/zxing/qrcode/encoder/QRCode.cs ================================================ /* * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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. */ using System; using System.Text; namespace ZXing.QrCode.Internal { /// satorux@google.com (Satoru Takabayashi) - creator /// dswitkin@google.com (Daniel Switkin) - ported from C++ public sealed class QRCode { /// /// /// public static int NUM_MASK_PATTERNS = 8; /// /// Initializes a new instance of the class. /// public QRCode() { MaskPattern = -1; } /// /// Gets or sets the mode. /// /// /// The mode. /// public Mode Mode { get; set; } /// /// Gets or sets the EC level. /// /// /// The EC level. /// public ErrorCorrectionLevel ECLevel { get; set; } /// /// Gets or sets the version. /// /// /// The version. /// public Version Version { get; set; } /// /// Gets or sets the mask pattern. /// /// /// The mask pattern. /// public int MaskPattern { get; set; } /// /// Gets or sets the matrix. /// /// /// The matrix. /// public ByteMatrix Matrix { get; set; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override String ToString() { var result = new StringBuilder(200); result.Append("<<\n"); result.Append(" mode: "); result.Append(Mode); result.Append("\n ecLevel: "); result.Append(ECLevel); result.Append("\n version: "); if (Version == null) result.Append("null"); else result.Append(Version); result.Append("\n maskPattern: "); result.Append(MaskPattern); if (Matrix == null) { result.Append("\n matrix: null\n"); } else { result.Append("\n matrix:\n"); result.Append(Matrix.ToString()); } result.Append(">>\n"); return result.ToString(); } /// /// Check if "mask_pattern" is valid. /// /// The mask pattern. /// /// true if [is valid mask pattern] [the specified mask pattern]; otherwise, false. /// public static bool isValidMaskPattern(int maskPattern) { return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS; } } } ================================================ FILE: shadowsocks-csharp/Controller/APIServer.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using Shadowsocks.Model; using Shadowsocks.Properties; using Shadowsocks.Util; using System.Web; namespace Shadowsocks.Controller { class APIServer : Listener.Service { private ShadowsocksController _controller; private Configuration _config; public const int RecvSize = 16384; private byte[] connetionRecvBuffer = new byte[RecvSize]; string connection_request; Socket _local; public APIServer(ShadowsocksController controller, Configuration config) { _controller = controller; _config = config; } public bool Handle(byte[] firstPacket, int length, Socket socket) { try { string request = Encoding.UTF8.GetString(firstPacket, 0, length); string[] lines = request.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); bool hostMatch = false, pathMatch = false; string req = ""; 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.Length == 1) { if (line.IndexOf("auth=" + _config.localAuthPassword) > 0) { if (line.IndexOf(" /api?") > 0) { req = line.Substring(line.IndexOf("api?") + 4); if (line.IndexOf("GET ") == 0 || line.IndexOf("POST ") == 0) { pathMatch = true; req = req.Substring(0, req.IndexOf(" ")); } } } } } if (hostMatch && pathMatch) { _local = socket; if (CheckEnd(request)) { process(request); } else { connection_request = request; socket.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, new AsyncCallback(HttpHandshakeRecv), null); } return true; } return false; } catch (ArgumentException) { return false; } } private bool CheckEnd(string request) { int newline_pos = request.IndexOf("\r\n\r\n"); if (request.StartsWith("POST ")) { if (newline_pos > 0) { string head = request.Substring(0, newline_pos); string tail = request.Substring(newline_pos + 4); string[] lines = head.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { if (line.StartsWith("Content-Length: ")) { try { int length = int.Parse(line.Substring("Content-Length: ".Length)); if (length <= tail.Length) return true; } catch (FormatException) { break; } } } return false; } } else { if (newline_pos + 4 == request.Length) { return true; } } return false; } private void HttpHandshakeRecv(IAsyncResult ar) { try { int bytesRead = _local.EndReceive(ar); if (bytesRead > 0) { string request = Encoding.UTF8.GetString(connetionRecvBuffer, 0, bytesRead); connection_request += request; if (CheckEnd(connection_request)) { process(connection_request); } else { _local.BeginReceive(connetionRecvBuffer, 0, RecvSize, 0, new AsyncCallback(HttpHandshakeRecv), null); } } else { Console.WriteLine("APIServer: failed to recv data in HttpHandshakeRecv"); _local.Shutdown(SocketShutdown.Both); _local.Close(); } } catch (Exception e) { Logging.LogUsefulException(e); try { _local.Shutdown(SocketShutdown.Both); _local.Close(); } catch { } } } protected string process(string request) { string req; req = request.Substring(0, request.IndexOf("\r\n")); req = req.Substring(req.IndexOf("api?") + 4); req = req.Substring(0, req.IndexOf(" ")); string[] get_params = req.Split('&'); Dictionary params_dict = new Dictionary(); foreach (string p in get_params) { if (p.IndexOf('=') > 0) { int index = p.IndexOf('='); string key, val; key = p.Substring(0, index); val = p.Substring(index + 1); params_dict[key] = val; } } if (request.IndexOf("POST ") == 0) { string post_params = request.Substring(request.IndexOf("\r\n\r\n") + 4); get_params = post_params.Split('&'); foreach (string p in get_params) { if (p.IndexOf('=') > 0) { int index = p.IndexOf('='); string key, val; key = p.Substring(0, index); val = p.Substring(index + 1); params_dict[key] = Util.Utils.urlDecode(val); } } } if (params_dict.ContainsKey("token") && params_dict.ContainsKey("app") && _config.token.ContainsKey(params_dict["app"]) && _config.token[params_dict["app"]] == params_dict["token"]) { if (params_dict.ContainsKey("action")) { if (params_dict["action"] == "statistics") { Configuration config = _config; ServerSpeedLogShow[] _ServerSpeedLogList = new ServerSpeedLogShow[config.configs.Count]; Dictionary servers = new Dictionary(); for (int i = 0; i < config.configs.Count; ++i) { _ServerSpeedLogList[i] = config.configs[i].ServerSpeedLog().Translate(); servers[config.configs[i].id] = _ServerSpeedLogList[i]; } string content = SimpleJson.SimpleJson.SerializeObject(servers); string text = String.Format(@"HTTP/1.1 200 OK Server: ShadowsocksR Content-Type: text/plain Content-Length: {0} Connection: Close ", System.Text.Encoding.UTF8.GetBytes(content).Length) + content; byte[] response = System.Text.Encoding.UTF8.GetBytes(text); _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local); return ""; } else if (params_dict["action"] == "config") { if (params_dict.ContainsKey("config")) { string content = ""; string ret_code = "200 OK"; if (!_controller.SaveServersConfig(params_dict["config"])) { ret_code = "403 Forbid"; } string text = String.Format(@"HTTP/1.1 {0} Server: ShadowsocksR Content-Type: text/plain Content-Length: {1} Connection: Close ", ret_code, System.Text.Encoding.UTF8.GetBytes(content).Length) + content; byte[] response = System.Text.Encoding.UTF8.GetBytes(text); _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local); return ""; } else { Dictionary token = _config.token; _config.token = new Dictionary(); string content = SimpleJson.SimpleJson.SerializeObject(_config); _config.token = token; string text = String.Format(@"HTTP/1.1 200 OK Server: ShadowsocksR Content-Type: text/plain Content-Length: {0} Connection: Close ", System.Text.Encoding.UTF8.GetBytes(content).Length) + content; byte[] response = System.Text.Encoding.UTF8.GetBytes(text); _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local); return ""; } } } } { byte[] response = System.Text.Encoding.UTF8.GetBytes(""); _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local); } return ""; } private void SendCallback(IAsyncResult ar) { Socket conn = (Socket)ar.AsyncState; try { conn.Shutdown(SocketShutdown.Both); conn.Close(); } catch { } } } } ================================================ FILE: shadowsocks-csharp/Controller/AutoStartup.cs ================================================ using System; using System.Windows.Forms; using Microsoft.Win32; namespace Shadowsocks.Controller { class AutoStartup { static string Key = "ShadowsocksR_" + Application.StartupPath.GetHashCode(); static string RegistryRunPath = (IntPtr.Size == 4 ? @"Software\Microsoft\Windows\CurrentVersion\Run" : @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run"); public static bool Set(bool enabled) { RegistryKey runKey = null; try { string path = Util.Utils.GetExecutablePath(); runKey = Registry.LocalMachine.OpenSubKey(RegistryRunPath, true); if (enabled) { runKey.SetValue(Key, path); } else { runKey.DeleteValue(Key); } runKey.Close(); return true; } catch //(Exception e) { //Logging.LogUsefulException(e); return Util.Utils.RunAsAdmin("--setautorun") == 0; } finally { if (runKey != null) { try { runKey.Close(); } catch (Exception e) { Logging.LogUsefulException(e); } } } } public static bool Switch() { bool enabled = !Check(); RegistryKey runKey = null; try { string path = Util.Utils.GetExecutablePath(); runKey = Registry.LocalMachine.OpenSubKey(RegistryRunPath, true); if (enabled) { runKey.SetValue(Key, path); } else { runKey.DeleteValue(Key); } runKey.Close(); return true; } catch (Exception e) { Logging.LogUsefulException(e); return false; } finally { if (runKey != null) { try { runKey.Close(); } catch (Exception e) { Logging.LogUsefulException(e); } } } } public static bool Check() { RegistryKey runKey = null; try { string path = Util.Utils.GetExecutablePath(); runKey = Registry.LocalMachine.OpenSubKey(RegistryRunPath, false); string[] runList = runKey.GetValueNames(); runKey.Close(); foreach (string item in runList) { if (item.Equals(Key)) return true; } return false; } catch (Exception e) { Logging.LogUsefulException(e); return false; } finally { if (runKey != null) { try { runKey.Close(); } catch (Exception e) { Logging.LogUsefulException(e); } } } } } } ================================================ FILE: shadowsocks-csharp/Controller/FileManager.cs ================================================ using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Text; namespace Shadowsocks.Controller { public class FileManager { public static bool ByteArrayToFile(string fileName, byte[] content) { try { System.IO.FileStream _FileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write); _FileStream.Write(content, 0, content.Length); _FileStream.Close(); return true; } catch (Exception _Exception) { Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); } return false; } public static void UncompressFile(string fileName, byte[] content) { FileStream destinationFile = File.Create(fileName); // Because the uncompressed size of the file is unknown, // we are using an arbitrary buffer size. byte[] buffer = new byte[4096]; int n; using (GZipStream input = new GZipStream(new MemoryStream(content), CompressionMode.Decompress, false)) { while (true) { n = input.Read(buffer, 0, buffer.Length); if (n == 0) { break; } destinationFile.Write(buffer, 0, n); } } destinationFile.Close(); } public static byte[] DeflateCompress(byte[] content, int index, int count, out int size) { size = 0; try { MemoryStream memStream = new MemoryStream(); using (DeflateStream ds = new DeflateStream(memStream, CompressionMode.Compress)) { ds.Write(content, index, count); } byte[] buffer = memStream.ToArray(); size = buffer.Length; return buffer; } catch (Exception _Exception) { Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); } return null; } public static byte[] DeflateDecompress(byte[] content, int index, int count, out int size) { size = 0; try { byte[] buffer = new byte[16384]; DeflateStream ds = new DeflateStream(new MemoryStream(content, index, count), CompressionMode.Decompress); int readsize; while (true) { readsize = ds.Read(buffer, size, buffer.Length - size); if (readsize == 0) { break; } size += readsize; byte[] newbuffer = new byte[buffer.Length * 2]; buffer.CopyTo(newbuffer, 0); buffer = newbuffer; } return buffer; } catch (Exception _Exception) { Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); } return null; } } } ================================================ FILE: shadowsocks-csharp/Controller/GfwListUpdater.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using Shadowsocks.Model; namespace Shadowsocks.Controller { public class GFWListUpdater { private const string GFWLIST_URL = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"; private const string GFWLIST_BACKUP_URL = "https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/gfwlist.txt"; private const string GFWLIST_TEMPLATE_URL = "https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/ss_gfw.pac"; private static string PAC_FILE = PACServer.PAC_FILE; private static string USER_RULE_FILE = PACServer.USER_RULE_FILE; private static string USER_ABP_FILE = PACServer.USER_ABP_FILE; private static string gfwlist_template = null; private Configuration lastConfig; public int update_type; public event EventHandler UpdateCompleted; public event ErrorEventHandler Error; public class ResultEventArgs : EventArgs { public bool Success; public ResultEventArgs(bool success) { this.Success = success; } } private void http_DownloadGFWTemplateCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string result = e.Result; if (result.IndexOf("__RULES__") > 0 && result.IndexOf("FindProxyForURL") > 0) { gfwlist_template = result; if (lastConfig != null) { UpdatePACFromGFWList(lastConfig); } lastConfig = null; } else { Error(this, new ErrorEventArgs(new Exception("Download ERROR"))); } } catch (Exception ex) { if (Error != null) { Error(this, new ErrorEventArgs(ex)); } } } private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { List lines = ParseResult(e.Result); if (lines.Count == 0) { throw new Exception("Empty GFWList"); } 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 = gfwlist_template; if (File.Exists(USER_ABP_FILE)) { abpContent = File.ReadAllText(USER_ABP_FILE, Encoding.UTF8); } else { abpContent = gfwlist_template; } abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines)); if (File.Exists(PAC_FILE)) { string original = File.ReadAllText(PAC_FILE, Encoding.UTF8); if (original == abpContent) { update_type = 0; UpdateCompleted(this, new ResultEventArgs(false)); return; } } File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8); if (UpdateCompleted != null) { update_type = 0; UpdateCompleted(this, new ResultEventArgs(true)); } } catch (Exception ex) { if (Error != null) { WebClient http = sender as WebClient; if (http.BaseAddress.StartsWith(GFWLIST_URL)) { http.BaseAddress = GFWLIST_BACKUP_URL; http.DownloadStringAsync(new Uri(GFWLIST_BACKUP_URL + "?rnd=" + Util.Utils.RandUInt32().ToString())); } else { if (e.Error != null) { Error(this, new ErrorEventArgs(e.Error)); } else { Error(this, new ErrorEventArgs(ex)); } } } } } private void http_DownloadPACCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string content = e.Result; if (File.Exists(PAC_FILE)) { string original = File.ReadAllText(PAC_FILE, Encoding.UTF8); if (original == content) { update_type = 1; UpdateCompleted(this, new ResultEventArgs(false)); return; } } File.WriteAllText(PAC_FILE, content, Encoding.UTF8); if (UpdateCompleted != null) { update_type = 1; UpdateCompleted(this, new ResultEventArgs(true)); } } catch (Exception ex) { if (Error != null) { Error(this, new ErrorEventArgs(ex)); } } } public void UpdatePACFromGFWList(Configuration config) { if (gfwlist_template == null) { lastConfig = config; WebClient http = new WebClient(); http.Headers.Add("User-Agent", String.IsNullOrEmpty(config.proxyUserAgent) ? "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36" : config.proxyUserAgent); WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); if (!string.IsNullOrEmpty(config.authPass)) { proxy.Credentials = new NetworkCredential(config.authUser, config.authPass); } http.Proxy = proxy; http.DownloadStringCompleted += http_DownloadGFWTemplateCompleted; http.DownloadStringAsync(new Uri(GFWLIST_TEMPLATE_URL + "?rnd=" + Util.Utils.RandUInt32().ToString())); } else { WebClient http = new WebClient(); http.Headers.Add("User-Agent", String.IsNullOrEmpty(config.proxyUserAgent) ? "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36" : config.proxyUserAgent); WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); if (!string.IsNullOrEmpty(config.authPass)) { proxy.Credentials = new NetworkCredential(config.authUser, config.authPass); } http.Proxy = proxy; http.BaseAddress = GFWLIST_URL; http.DownloadStringCompleted += http_DownloadStringCompleted; http.DownloadStringAsync(new Uri(GFWLIST_URL + "?rnd=" + Util.Utils.RandUInt32().ToString())); } } public void UpdatePACFromGFWList(Configuration config, string url) { WebClient http = new WebClient(); http.Headers.Add("User-Agent", String.IsNullOrEmpty(config.proxyUserAgent) ? "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36" : config.proxyUserAgent); WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); if (!string.IsNullOrEmpty(config.authPass)) { proxy.Credentials = new NetworkCredential(config.authUser, config.authPass); } http.Proxy = proxy; http.DownloadStringCompleted += http_DownloadPACCompleted; http.DownloadStringAsync(new Uri(url + "?rnd=" + Util.Utils.RandUInt32().ToString())); } public List 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 valid_lines = new List(lines.Length); foreach (string line in lines) { if (line.StartsWith("!") || line.StartsWith("[")) continue; valid_lines.Add(line); } return valid_lines; } } } ================================================ FILE: shadowsocks-csharp/Controller/HttpPortForwarder.cs ================================================ using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Shadowsocks.Model; namespace Shadowsocks.Controller { class HttpPortForwarder : Listener.Service { int _targetPort; Configuration _config; public HttpPortForwarder(int targetPort, Configuration config) { this._targetPort = targetPort; this._config = config; } public bool Handle(byte[] firstPacket, int length, Socket socket) { new Handler().Start(_config, firstPacket, length, socket, this._targetPort); return true; } class Handler { private byte[] _firstPacket; private int _firstPacketLength; private int _targetPort; private Socket _local; private Socket _remote; private bool _closed = false; private Configuration _config; HttpPraser httpProxyState; public const int RecvSize = 4096; // remote receive buffer private byte[] remoteRecvBuffer = new byte[RecvSize]; // connection receive buffer private byte[] connetionRecvBuffer = new byte[RecvSize]; public void Start(Configuration config, byte[] firstPacket, int length, Socket socket, int targetPort) { _firstPacket = firstPacket; _firstPacketLength = length; _local = socket; _targetPort = targetPort; _config = config; if ((_config.authUser ?? "").Length == 0 || Util.Utils.isMatchSubNet(((IPEndPoint)this._local.RemoteEndPoint).Address, "127.0.0.0/8")) { Connect(); } else { RspHttpHandshakeReceive(); } } private void RspHttpHandshakeReceive() { if (httpProxyState == null) { httpProxyState = new HttpPraser(true); } httpProxyState.httpAuthUser = _config.authUser; httpProxyState.httpAuthPass = _config.authPass; byte[] remoteHeaderSendBuffer = null; int err = httpProxyState.HandshakeReceive(_firstPacket, _firstPacketLength, ref remoteHeaderSendBuffer); if (err == 1) { _local.BeginReceive(connetionRecvBuffer, 0, _firstPacket.Length, 0, new AsyncCallback(HttpHandshakeRecv), null); } else if (err == 2) { string dataSend = httpProxyState.Http407(); byte[] httpData = System.Text.Encoding.UTF8.GetBytes(dataSend); _local.BeginSend(httpData, 0, httpData.Length, 0, new AsyncCallback(HttpHandshakeAuthEndSend), null); } else if (err == 3) { Connect(); } else if (err == 4) { Connect(); } else if (err == 0) { string dataSend = httpProxyState.Http200(); byte[] httpData = System.Text.Encoding.UTF8.GetBytes(dataSend); _local.BeginSend(httpData, 0, httpData.Length, 0, new AsyncCallback(StartConnect), null); } else if (err == 500) { string dataSend = httpProxyState.Http500(); byte[] httpData = System.Text.Encoding.UTF8.GetBytes(dataSend); _local.BeginSend(httpData, 0, httpData.Length, 0, new AsyncCallback(HttpHandshakeAuthEndSend), null); } } private void HttpHandshakeRecv(IAsyncResult ar) { if (_closed) { return; } try { int bytesRead = _local.EndReceive(ar); if (bytesRead > 0) { Array.Copy(connetionRecvBuffer, _firstPacket, bytesRead); _firstPacketLength = bytesRead; RspHttpHandshakeReceive(); } else { Console.WriteLine("failed to recv data in HttpHandshakeRecv"); Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void HttpHandshakeAuthEndSend(IAsyncResult ar) { if (_closed) { return; } try { _local.EndSend(ar); _local.BeginReceive(connetionRecvBuffer, 0, _firstPacket.Length, 0, new AsyncCallback(HttpHandshakeRecv), null); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void StartConnect(IAsyncResult ar) { try { _local.EndSend(ar); Connect(); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void Connect() { try { 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.NoDelay = true; // Connect to the remote endpoint. _remote.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void ConnectCallback(IAsyncResult ar) { if (_closed) { return; } try { _remote.EndConnect(ar); HandshakeReceive(); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void HandshakeReceive() { if (_closed) { return; } try { _remote.BeginSend(_firstPacket, 0, _firstPacketLength, 0, new AsyncCallback(StartPipe), null); } catch (Exception e) { Logging.LogUsefulException(e); 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); 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 { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); 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 { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); 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); 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); Close(); } } public void Close() { lock (this) { if (_closed) { return; } _closed = true; } Thread.Sleep(100); 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); } } } } } } ================================================ FILE: shadowsocks-csharp/Controller/HttpProxy.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; namespace Shadowsocks.Controller { class HttpPraser { public bool httpProxy = false; public byte[] httpRequestBuffer; public int httpContentLength = 0; //public byte[] lastContentBuffer; public string httpAuthUser; public string httpAuthPass; protected string httpHost; protected int httpPort; bool redir; public HttpPraser(bool redir = false) { this.redir = redir; } private static string ParseHostAndPort(string str, ref int port) { string host; if (str.StartsWith("[")) { int pos = str.LastIndexOf(']'); if (pos > 0) { host = str.Substring(1, pos - 1); if (str.Length > pos + 1 && str[pos + 2] == ':') { port = Convert.ToInt32(str.Substring(pos + 2)); } } else { host = str; } } else { int pos = str.LastIndexOf(':'); if (pos > 0) { host = str.Substring(0, pos); port = Convert.ToInt32(str.Substring(pos + 1)); } else { host = str; } } return host; } protected string ParseURL(string url, string host, int port) { if (url.StartsWith("http://")) { url = url.Substring(7); } if (url.StartsWith("[")) { if (url.StartsWith("[" + host + "]")) { url = url.Substring(host.Length + 2); } } else if (url.StartsWith(host)) { url = url.Substring(host.Length); } if (url.StartsWith(":")) { if (url.StartsWith(":" + port.ToString())) { url = url.Substring((":" + port.ToString()).Length); } } if (!url.StartsWith("/")) { int pos_slash = url.IndexOf('/'); int pos_space = url.IndexOf(' '); if (pos_slash > 0 && pos_slash < pos_space) { url = url.Substring(pos_slash); } } if (url.StartsWith(" ")) { url = "/" + url; } return url; } public void HostToHandshakeBuffer(string host, int port, ref byte[] remoteHeaderSendBuffer) { if (redir) { remoteHeaderSendBuffer = new byte[0]; } else if (host.Length > 0) { IPAddress ipAddress; bool parsed = IPAddress.TryParse(host, out ipAddress); if (!parsed) { remoteHeaderSendBuffer = new byte[2 + host.Length + 2]; remoteHeaderSendBuffer[0] = 3; remoteHeaderSendBuffer[1] = (byte)host.Length; System.Text.Encoding.UTF8.GetBytes(host).CopyTo(remoteHeaderSendBuffer, 2); } else { if (ipAddress.AddressFamily == AddressFamily.InterNetwork) { remoteHeaderSendBuffer = new byte[7]; remoteHeaderSendBuffer[0] = 1; ipAddress.GetAddressBytes().CopyTo(remoteHeaderSendBuffer, 1); } else { remoteHeaderSendBuffer = new byte[19]; remoteHeaderSendBuffer[0] = 4; ipAddress.GetAddressBytes().CopyTo(remoteHeaderSendBuffer, 1); } } remoteHeaderSendBuffer[remoteHeaderSendBuffer.Length - 2] = (byte)(port >> 8); remoteHeaderSendBuffer[remoteHeaderSendBuffer.Length - 1] = (byte)(port & 0xff); } } protected int AppendRequest(ref byte[] Packet, ref int PacketLength) { if (httpContentLength > 0) { if (httpContentLength >= PacketLength) { httpContentLength -= PacketLength; PacketLength = 0; Packet = new byte[0]; return -1; } else { int len = PacketLength - httpContentLength; byte[] nextbuffer = new byte[len]; Array.Copy(Packet, httpContentLength, nextbuffer, 0, len); Packet = nextbuffer; PacketLength -= httpContentLength; httpContentLength = 0; } } byte[] block = new byte[] { (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' }; int pos; if (httpRequestBuffer == null) { httpRequestBuffer = new byte[PacketLength]; } else { Array.Resize(ref httpRequestBuffer, httpRequestBuffer.Length + PacketLength); } Array.Copy(Packet, 0, httpRequestBuffer, httpRequestBuffer.Length - PacketLength, PacketLength); pos = Util.Utils.FindStr(httpRequestBuffer, httpRequestBuffer.Length, block); return pos; } protected Dictionary ParseHttpHeader(string header) { Dictionary header_dict = new Dictionary(); string[] lines = header.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string[] cmdItems = lines[0].Split(new[] { ' ' }, 3); for (int index = 1; index < lines.Length; ++index) { string[] parts = lines[index].Split(new string[] { ": " }, 2, StringSplitOptions.RemoveEmptyEntries); if (parts.Length > 1) { header_dict[parts[0]] = parts[1]; } } header_dict["@0"] = cmdItems[0]; header_dict["@1"] = cmdItems[1]; header_dict["@2"] = cmdItems[2]; return header_dict; } protected string HeaderDictToString(Dictionary dict) { string cmd = ""; string result = ""; cmd = dict["@0"] + " " + dict["@1"] + " " + dict["@2"] + "\r\n"; dict.Remove("@0"); dict.Remove("@1"); dict.Remove("@2"); result += "Host" + ": " + dict["Host"] + "\r\n"; dict.Remove("Host"); foreach (KeyValuePair it in dict) { result += it.Key + ": " + it.Value + "\r\n"; } return cmd + result + "\r\n"; } public int HandshakeReceive(byte[] _firstPacket, int _firstPacketLength, ref byte[] remoteHeaderSendBuffer) { remoteHeaderSendBuffer = null; int pos = AppendRequest(ref _firstPacket, ref _firstPacketLength); if (pos < 0) { return 1; } string data = System.Text.Encoding.UTF8.GetString(httpRequestBuffer, 0, pos + 4); { byte[] nextbuffer = new byte[httpRequestBuffer.Length - (pos + 4)]; Array.Copy(httpRequestBuffer, pos + 4, nextbuffer, 0, nextbuffer.Length); httpRequestBuffer = nextbuffer; } Dictionary header_dict = ParseHttpHeader(data); this.httpPort = 80; if (header_dict["@0"] == "CONNECT") { this.httpHost = ParseHostAndPort(header_dict["@1"], ref this.httpPort); } else if (header_dict.ContainsKey("Host")) { this.httpHost = ParseHostAndPort(header_dict["Host"], ref this.httpPort); } else { return 500; } if (header_dict.ContainsKey("Content-Length") && Convert.ToInt32(header_dict["Content-Length"]) > 0) { httpContentLength = Convert.ToInt32(header_dict["Content-Length"]) + 2; } HostToHandshakeBuffer(this.httpHost, this.httpPort, ref remoteHeaderSendBuffer); if (redir) { if (header_dict.ContainsKey("Proxy-Connection")) { header_dict["Connection"] = header_dict["Proxy-Connection"]; header_dict.Remove("Proxy-Connection"); } string httpRequest = HeaderDictToString(header_dict); int len = remoteHeaderSendBuffer.Length; byte[] httpData = System.Text.Encoding.UTF8.GetBytes(httpRequest); Array.Resize(ref remoteHeaderSendBuffer, len + httpData.Length); httpData.CopyTo(remoteHeaderSendBuffer, len); httpProxy = true; } bool auth_ok = false; if (httpAuthUser == null || httpAuthUser.Length == 0) { auth_ok = true; } if (header_dict.ContainsKey("Proxy-Authorization")) { string authString = header_dict["Proxy-Authorization"].Substring("Basic ".Length); string authStr = httpAuthUser + ":" + (httpAuthPass ?? ""); string httpAuthString = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(authStr)); if (httpAuthString == authString) { auth_ok = true; } header_dict.Remove("Proxy-Authorization"); } if (auth_ok && httpRequestBuffer.Length > 0) { int len = httpRequestBuffer.Length; byte[] httpData = httpRequestBuffer; Array.Resize(ref remoteHeaderSendBuffer, len + remoteHeaderSendBuffer.Length); httpData.CopyTo(remoteHeaderSendBuffer, remoteHeaderSendBuffer.Length - len); httpRequestBuffer = new byte[0]; } if (auth_ok && httpContentLength > 0) { int len = Math.Min(httpRequestBuffer.Length, httpContentLength); Array.Resize(ref remoteHeaderSendBuffer, len + remoteHeaderSendBuffer.Length); Array.Copy(httpRequestBuffer, 0, remoteHeaderSendBuffer, remoteHeaderSendBuffer.Length - len, len); byte[] nextbuffer = new byte[httpRequestBuffer.Length - len]; Array.Copy(httpRequestBuffer, len, nextbuffer, 0, nextbuffer.Length); httpRequestBuffer = nextbuffer; } else { httpContentLength = 0; httpRequestBuffer = new byte[0]; } if (remoteHeaderSendBuffer == null || !auth_ok) { return 2; } if (httpProxy) { return 3; } return 0; } public string Http200() { return "HTTP/1.1 200 Connection Established\r\n\r\n"; } public string Http407() { string header = "HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"RRR\"\r\n"; string content = "" + "" + " " + " Error" + " " + " " + "

407 Proxy Authentication Required.

" + "\r\n"; return header + "\r\n" + content + "\r\n"; } public string Http500() { string header = "HTTP/1.1 500 Internal Server Error\r\n"; string content = "" + "" + " " + " Error" + " " + " " + "

500 Internal Server Error.

" + ""; return header + "\r\n" + content + "\r\n"; } } } ================================================ FILE: shadowsocks-csharp/Controller/HttpProxyRunner.cs ================================================ 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 System.Windows.Forms; namespace Shadowsocks.Controller { class HttpProxyRunner { private Process _process; private static string runningPath; private int _runningPort; private static string _subPath = @"temp"; private static string _exeNameNoExt = @"/ssr_privoxy"; private static string _exeName = @"/ssr_privoxy.exe"; static HttpProxyRunner() { runningPath = Path.Combine(System.Windows.Forms.Application.StartupPath, _subPath); _exeNameNoExt = System.IO.Path.GetFileNameWithoutExtension(Util.Utils.GetExecutablePath()); _exeName = @"/" + _exeNameNoExt + @".exe"; if (!Directory.Exists(runningPath)) { Directory.CreateDirectory(runningPath); } Kill(); try { FileManager.UncompressFile(runningPath + _exeName, Resources.privoxy_exe); FileManager.UncompressFile(runningPath + "/mgwz.dll", Resources.mgwz_dll); } catch (IOException e) { Logging.LogUsefulException(e); } } public int RunningPort { get { return _runningPort; } } public bool HasExited() { if (_process == null) return true; try { return _process.HasExited; } catch { return false; } } public static void Kill() { Process[] existingPolipo = Process.GetProcessesByName(_exeNameNoExt); foreach (Process p in existingPolipo) { string str; try { str = p.MainModule.FileName; } catch (Exception) { continue; } if (str == Path.GetFullPath(runningPath + _exeName)) { try { p.Kill(); p.WaitForExit(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } public void Start(Configuration configuration) { if (_process == null) { Kill(); string polipoConfig = Resources.privoxy_conf; _runningPort = this.GetFreePort(); polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.ToString()); polipoConfig = polipoConfig.Replace("__PRIVOXY_BIND_PORT__", _runningPort.ToString()); polipoConfig = polipoConfig.Replace("__PRIVOXY_BIND_IP__", "127.0.0.1"); polipoConfig = polipoConfig.Replace("__BYPASS_ACTION__", ""); FileManager.ByteArrayToFile(runningPath + "/privoxy.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); Restart(); } } public void Restart() { _process = new Process(); // Configure the process using the StartInfo properties. _process.StartInfo.FileName = runningPath + _exeName; _process.StartInfo.Arguments = " \"" + runningPath + "/privoxy.conf\""; _process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; _process.StartInfo.UseShellExecute = true; _process.StartInfo.CreateNoWindow = true; _process.StartInfo.WorkingDirectory = System.Windows.Forms.Application.StartupPath; //_process.StartInfo.RedirectStandardOutput = true; //_process.StartInfo.RedirectStandardError = true; try { _process.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } public void Stop() { if (_process != null) { try { _process.Kill(); _process.WaitForExit(); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { _process = null; } } } private int GetFreePort() { int defaultPort = 60000; try { IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); Random random = new Random(Util.Utils.GetExecutablePath().GetHashCode() ^ (int)DateTime.Now.Ticks); List usedPorts = new List(); foreach (IPEndPoint endPoint in IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()) { usedPorts.Add(endPoint.Port); } for (int nTry = 0; nTry < 1000; nTry++) { int port = random.Next(10000, 65536); 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."); } } } ================================================ FILE: shadowsocks-csharp/Controller/I18N.cs ================================================ using Shadowsocks.Properties; using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Shadowsocks.Controller { public class I18N { protected static Dictionary Strings; static void Init(string res) { string[] lines = Regex.Split(res, "\r\n|\r|\n"); foreach (string line in lines) { if (line.StartsWith("#")) { continue; } string[] kv = Regex.Split(line, "="); if (kv.Length == 2) { string val = Regex.Replace(kv[1], "\\\\n", "\r\n"); Strings[kv[0]] = val; } } } static I18N() { Strings = new Dictionary(); //if (System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag.ToLowerInvariant().StartsWith("zh")) string name = System.Globalization.CultureInfo.CurrentCulture.Name; if (name.StartsWith("zh")) { if (name == "zh" || name == "zh-CN") { Init(Resources.cn); } else { Init(Resources.zh_tw); } } } public static string GetString(string key) { if (Strings.ContainsKey(key)) { return Strings[key]; } else { return key; } } } } ================================================ FILE: shadowsocks-csharp/Controller/Listener.cs ================================================ using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Timers; namespace Shadowsocks.Controller { public class Listener { public interface Service { bool Handle(byte[] firstPacket, int length, Socket socket); } Configuration _config; bool _shareOverLAN; string _authUser; string _authPass; Socket _socket; Socket _socket_v6; bool _stop; IList _services; protected System.Timers.Timer timer; protected object timerLock = new object(); public Listener(IList services) { this._services = services; _stop = false; } public IList GetServices() { return _services; } private bool CheckIfPortInUse(int port) { try { IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { return true; } } } catch { } return false; } public bool isConfigChange(Configuration config) { try { if (this._shareOverLAN != config.shareOverLan || _authUser != config.authUser || _authPass != config.authPass || _socket == null || ((IPEndPoint)_socket.LocalEndPoint).Port != config.localPort) { return true; } } catch (Exception) { } return false; } public void Start(Configuration config, int port) { this._config = config; this._shareOverLAN = config.shareOverLan; this._authUser = config.authUser; this._authPass = config.authPass; _stop = false; int localPort = port == 0 ? _config.localPort : port; if (CheckIfPortInUse(localPort)) throw new Exception(I18N.GetString("Port already in use")); try { // Create a TCP/IP socket. bool ipv6 = true; //bool ipv6 = false; _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); if (ipv6) { try { _socket_v6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); //_socket_v6.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false); _socket_v6.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } catch { _socket_v6 = null; } } IPEndPoint localEndPoint = null; IPEndPoint localEndPointV6 = null; localEndPoint = new IPEndPoint(IPAddress.Any, localPort); localEndPointV6 = new IPEndPoint(IPAddress.IPv6Any, localPort); // Bind the socket to the local endpoint and listen for incoming connections. if (_socket_v6 != null) { _socket_v6.Bind(localEndPointV6); _socket_v6.Listen(1024); } //try { //throw new SocketException(); _socket.Bind(localEndPoint); _socket.Listen(1024); } //catch (SocketException e) //{ // if (_socket_v6 == null) // { // throw e; // } // else // { // _socket.Close(); // _socket = _socket_v6; // _socket_v6 = null; // } //} // Start an asynchronous socket to listen for connections. Console.WriteLine("ShadowsocksR started on port " + localPort.ToString()); _socket.BeginAccept( new AsyncCallback(AcceptCallback), _socket); if (_socket_v6 != null) _socket_v6.BeginAccept( new AsyncCallback(AcceptCallback), _socket_v6); } catch (SocketException e) { Logging.LogUsefulException(e); if (_socket != null) { _socket.Close(); _socket = null; } if (_socket_v6 != null) { _socket_v6.Close(); _socket_v6 = null; } throw; } } public void Stop() { ResetTimeout(0, null); _stop = true; if (_socket != null) { _socket.Close(); _socket = null; } if (_socket_v6 != null) { _socket_v6.Close(); _socket_v6 = null; } } private void ResetTimeout(Double time, Socket socket) { if (time <= 0 && timer == null) return; lock (timerLock) { if (time <= 0) { if (timer != null) { timer.Enabled = false; timer.Elapsed -= (sender, e) => timer_Elapsed(sender, e, socket); timer.Dispose(); timer = null; } } else { if (timer == null) { timer = new System.Timers.Timer(time * 1000.0); timer.Elapsed += (sender, e) => timer_Elapsed(sender, e, socket); timer.Start(); } else { timer.Interval = time * 1000.0; timer.Stop(); timer.Start(); } } } } private void timer_Elapsed(object sender, ElapsedEventArgs eventArgs, Socket socket) { if (timer == null) { return; } Socket listener = socket; try { listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); ResetTimeout(0, listener); } catch (ObjectDisposedException) { // do nothing } catch (Exception e) { Logging.LogUsefulException(e); ResetTimeout(5, listener); } } public void AcceptCallback(IAsyncResult ar) { if (_stop) return; Socket listener = (Socket)ar.AsyncState; try { Socket conn = listener.EndAccept(ar); if (!_shareOverLAN && !Util.Utils.isLocal(conn)) { conn.Shutdown(SocketShutdown.Both); conn.Close(); } int local_port = ((IPEndPoint)conn.LocalEndPoint).Port; if ((_authUser ?? "").Length == 0 && !Util.Utils.isLAN(conn) && !(_config.GetPortMapCache().ContainsKey(local_port) || _config.GetPortMapCache()[local_port].type == PortMapType.Forward)) { conn.Shutdown(SocketShutdown.Both); conn.Close(); } else { byte[] buf = new byte[4096]; object[] state = new object[] { conn, buf }; if (!_config.GetPortMapCache().ContainsKey(local_port) || _config.GetPortMapCache()[local_port].type != PortMapType.Forward) { conn.BeginReceive(buf, 0, buf.Length, 0, new AsyncCallback(ReceiveCallback), state); } else { foreach (Service service in _services) { if (service.Handle(buf, 0, conn)) { return; } } // no service found for this // shouldn't happen conn.Shutdown(SocketShutdown.Both); conn.Close(); } } } 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); ResetTimeout(5, listener); } } } 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.Shutdown(SocketShutdown.Both); conn.Close(); } catch (Exception e) { Console.WriteLine(e); conn.Shutdown(SocketShutdown.Both); conn.Close(); } } } } ================================================ FILE: shadowsocks-csharp/Controller/Local.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using Shadowsocks.Encryption; using Shadowsocks.Obfs; using Shadowsocks.Model; using System.Timers; using System.Threading; using OpenDNS; using Shadowsocks.Util; namespace Shadowsocks.Controller { class CallbackStatus { protected int status; public CallbackStatus() { status = 0; } public void SetIfEqu(int newStatus, int oldStatus) { lock (this) { if (status == oldStatus) { status = newStatus; } } } public int Status { get { lock (this) { return status; } } set { lock (this) { status = value; } } } } class Local : Listener.Service { private delegate void InvokeHandler(); private Configuration _config; private ServerTransferTotal _transfer; private IPRangeSet _IPRange; public Local(Configuration config, ServerTransferTotal transfer, IPRangeSet IPRange) { _config = config; _transfer = transfer; _IPRange = IPRange; } protected bool Accept(byte[] firstPacket, int length) { if (length < 2) { return false; } if (firstPacket[0] == 5 || firstPacket[0] == 4) { return true; } if (length > 8 && firstPacket[0] == 'C' && firstPacket[1] == 'O' && firstPacket[2] == 'N' && firstPacket[3] == 'N' && firstPacket[4] == 'E' && firstPacket[5] == 'C' && firstPacket[6] == 'T' && firstPacket[7] == ' ') { return true; } return false; } public bool Handle(byte[] firstPacket, int length, Socket socket) { if (!_config.GetPortMapCache().ContainsKey(((IPEndPoint)socket.LocalEndPoint).Port) && !Accept(firstPacket, length)) { return false; } InvokeHandler handler = () => new ProxyAuthHandler(_config, _transfer, _IPRange, firstPacket, length, socket); handler.BeginInvoke(null, null); return true; } } class HandlerConfig : ICloneable { public string targetHost; public int targetPort; public Double TTL = 0; // Second public Double connect_timeout = 0; public int try_keep_alive = 0; public string local_dns_servers; public string dns_servers; public bool fouce_local_dns_query = false; // Server proxy public int proxyType = 0; public string socks5RemoteHost; public int socks5RemotePort = 0; public string socks5RemoteUsername; public string socks5RemotePassword; public string proxyUserAgent; // auto ban public bool autoSwitchOff = true; // Reconnect public int reconnectTimesRemain = 0; public int reconnectTimes = 0; public bool random = false; public bool forceRandom = false; public object Clone() { HandlerConfig obj = new HandlerConfig(); obj.targetHost = targetHost; obj.targetPort = targetPort; obj.TTL = TTL; obj.connect_timeout = connect_timeout; obj.try_keep_alive = try_keep_alive; obj.local_dns_servers = local_dns_servers; obj.dns_servers = dns_servers; obj.fouce_local_dns_query = fouce_local_dns_query; obj.proxyType = proxyType; obj.socks5RemoteHost = socks5RemoteHost; obj.socks5RemotePort = socks5RemotePort; obj.socks5RemoteUsername = socks5RemoteUsername; obj.socks5RemotePassword = socks5RemotePassword; obj.proxyUserAgent = proxyUserAgent; obj.autoSwitchOff = autoSwitchOff; obj.reconnectTimesRemain = reconnectTimesRemain; obj.reconnectTimes = reconnectTimes; obj.random = random; obj.forceRandom = forceRandom; return obj; } } class Handler : IHandler { private delegate IPHostEntry GetHostEntryHandler(string ip); public delegate Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc filter, string targetURI = null, bool cfgRandom = false, bool usingRandom = false, bool forceRandom = false); public delegate void KeepCurrentServer(int localPort, string targetURI, string id); public GetCurrentServer getCurrentServer; public KeepCurrentServer keepCurrentServer; public Server server; public ServerSelectStrategy.FilterFunc select_server; public HandlerConfig cfg = new HandlerConfig(); // Connection socket public ProxySocketTunLocal connection; public Socket connectionUDP; protected IPEndPoint connectionUDPEndPoint; protected int localPort; protected ProtocolResponseDetector detector = new ProtocolResponseDetector(); // remote socket. //protected Socket remote; protected ProxyEncryptSocket remote; protected ProxyEncryptSocket remoteUDP; // Size of receive buffer. protected const int RecvSize = ProxyEncryptSocket.MSS * 4; protected const int BufferSize = ProxyEncryptSocket.MSS * 16; // remote header send buffer protected byte[] remoteHeaderSendBuffer; // connection send buffer protected List connectionSendBufferList = new List(); protected DateTime lastKeepTime; private int _totalRecvSize = 0; protected byte[] remoteUDPRecvBuffer = new byte[BufferSize]; protected int remoteUDPRecvBufferLength = 0; protected object recvUDPoverTCPLock = new object(); protected bool closed = false; protected bool local_error = false; protected bool is_protocol_sendback = false; protected bool is_obfs_sendback = false; protected bool connectionTCPIdle, connectionUDPIdle, remoteTCPIdle, remoteUDPIdle; protected SpeedTester speedTester = new SpeedTester(); protected int lastErrCode; protected Random random = new Random(); protected System.Timers.Timer timer; protected object timerLock = new object(); protected DateTime lastTimerSetTime; enum ConnectState { END = -1, READY = 0, HANDSHAKE = 1, CONNECTING = 2, CONNECTED = 3, } private ConnectState state = ConnectState.READY; private ConnectState State { get { return this.state; } set { lock (this) { this.state = value; } } } private void ResetTimeout(double time, bool reset_keep_alive = true) { if (time <= 0 && timer == null) return; if (reset_keep_alive) cfg.try_keep_alive = 0; if (time <= 0) { if (timer != null) { lock (timerLock) { if (timer != null) { timer.Enabled = false; timer.Elapsed -= timer_Elapsed; timer.Dispose(); timer = null; } } } } else if (!closed) { if (lastTimerSetTime != null && (DateTime.Now - lastTimerSetTime).TotalMilliseconds > 500) { lock (timerLock) { if (timer == null) { timer = new System.Timers.Timer(time * 1000.0); timer.Elapsed += timer_Elapsed; } else { timer.Interval = time * 1000.0; timer.Stop(); } timer.Start(); lastTimerSetTime = DateTime.Now; } } } } private void timer_Elapsed(object sender, ElapsedEventArgs e) { if (closed) { return; } bool stop = false; try { if (cfg.try_keep_alive <= 0 && State == ConnectState.CONNECTED && remote != null && remoteUDP == null && remote.CanSendKeepAlive) { cfg.try_keep_alive++; RemoteSend(remoteUDPRecvBuffer, -1); } else { if (connection != null) { Server s = server; if (remote != null && cfg.reconnectTimesRemain > 0 //&& obfs != null && obfs.getSentLength() == 0 && connectionSendBufferList != null && (State == ConnectState.CONNECTED || State == ConnectState.CONNECTING)) { if (lastErrCode == 0) { if (State == ConnectState.CONNECTING && cfg.socks5RemotePort > 0) { } else { lastErrCode = 8; s.ServerSpeedLog().AddTimeoutTimes(); } } //remote.Shutdown(SocketShutdown.Both); stop = true; } else { if (s != null && connectionSendBufferList != null ) { if (lastErrCode == 0) { lastErrCode = 8; s.ServerSpeedLog().AddTimeoutTimes(); } } //connection.Shutdown(SocketShutdown.Both); stop = true; local_error = true; } } } } catch (Exception) { // } if (stop) { //Thread.Sleep(200); Close(); } } public void setServerTransferTotal(ServerTransferTotal transfer) { speedTester.transfer = transfer; } public int LogSocketException(Exception e) { // just log useful exceptions, not all of them Server s = server; if (e is ObfsException) { ObfsException oe = (ObfsException)e; if (lastErrCode == 0) { if (s != null) { lastErrCode = 16; s.ServerSpeedLog().AddErrorDecodeTimes(); } } return 16; // ObfsException(decrypt error) } else if (e is ProtocolException) { ProtocolException pe = (ProtocolException)e; if (lastErrCode == 0) { if (s != null) { lastErrCode = 16; s.ServerSpeedLog().AddErrorDecodeTimes(); } } return 16; // ObfsException(decrypt error) } else if (e is SocketException) { SocketException se = (SocketException)e; if (se.SocketErrorCode == SocketError.ConnectionAborted || se.SocketErrorCode == SocketError.ConnectionReset || se.SocketErrorCode == SocketError.NotConnected || se.SocketErrorCode == SocketError.Interrupted || se.SocketErrorCode == SocketError.Shutdown ) { // closed by browser when sending // normally happens when download is canceled or a tab is closed before page is loaded } else if (se.ErrorCode == 11004) { if (lastErrCode == 0) { if (s != null) { lastErrCode = 1; s.ServerSpeedLog().AddErrorTimes(); } } return 1; // proxy DNS error } else if (se.SocketErrorCode == SocketError.HostNotFound) { if (lastErrCode == 0) { if (s != null) { lastErrCode = 2; s.ServerSpeedLog().AddErrorTimes(); if (s.ServerSpeedLog().ErrorConnectTimes >= 3 && cfg.autoSwitchOff) { s.setEnable(false); } } } return 2; // ip not exist } else if (se.SocketErrorCode == SocketError.ConnectionRefused) { if (lastErrCode == 0) { if (s != null) { lastErrCode = 1; if (cfg != null && cfg.socks5RemotePort == 0) s.ServerSpeedLog().AddErrorTimes(); } } return 2; // proxy ip/port error } else if (se.SocketErrorCode == SocketError.NetworkUnreachable) { if (lastErrCode == 0) { if (s != null) { lastErrCode = 3; //s.ServerSpeedLog().AddErrorTimes(); } } return 3; // proxy ip/port error } else if (se.SocketErrorCode == SocketError.TimedOut) { if (lastErrCode == 0) { if (s != null) { lastErrCode = 8; s.ServerSpeedLog().AddTimeoutTimes(); } } return 8; // proxy server no response too slow } else { if (lastErrCode == 0) { lastErrCode = -1; if (s != null) s.ServerSpeedLog().AddNoErrorTimes(); //? } return -1; } } return 0; } public bool ReConnect() { Logging.Debug("Reconnect " + cfg.targetHost + ":" + cfg.targetPort.ToString() + " " + connection.GetSocket().Handle.ToString()); { Handler handler = new Handler(); handler.getCurrentServer = getCurrentServer; handler.keepCurrentServer = keepCurrentServer; handler.select_server = select_server; handler.connection = connection; handler.connectionUDP = connectionUDP; handler.cfg = cfg.Clone() as HandlerConfig; handler.cfg.reconnectTimesRemain = cfg.reconnectTimesRemain - 1; handler.cfg.reconnectTimes = cfg.reconnectTimes + 1; handler.speedTester.transfer = speedTester.transfer; int total_len = 0; byte[] newFirstPacket = remoteHeaderSendBuffer; if (connectionSendBufferList != null && connectionSendBufferList.Count > 0) { foreach (byte[] data in connectionSendBufferList) { total_len += data.Length; } newFirstPacket = new byte[total_len]; total_len = 0; foreach (byte[] data in connectionSendBufferList) { Buffer.BlockCopy(data, 0, newFirstPacket, total_len, data.Length); total_len += data.Length; } } handler.Start(newFirstPacket, newFirstPacket.Length, connection.local_sendback_protocol); } return true; } public void Start(byte[] firstPacket, int length, string rsp_protocol) { connection.local_sendback_protocol = rsp_protocol; if (cfg.socks5RemotePort > 0) { cfg.autoSwitchOff = false; } ResetTimeout(cfg.TTL); if (this.State == ConnectState.READY) { State = ConnectState.HANDSHAKE; remoteHeaderSendBuffer = firstPacket; detector.OnSend(remoteHeaderSendBuffer, length); byte[] data = new byte[length]; Array.Copy(remoteHeaderSendBuffer, data, data.Length); connectionSendBufferList.Add(data); remoteHeaderSendBuffer = data; if (cfg.reconnectTimes > 0) { InvokeHandler handler = () => Connect(); handler.BeginInvoke(null, null); } else { Connect(); } } else { Close(); } } private void BeginConnect(IPAddress ipAddress, int serverPort) { IPEndPoint remoteEP = new IPEndPoint(ipAddress, serverPort); if (cfg.socks5RemotePort != 0 || connectionUDP == null || connectionUDP != null && server.udp_over_tcp) { remote = new ProxyEncryptSocket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); remote.GetSocket().NoDelay = true; try { remote.CreateEncryptor(server.method, server.password); } catch { } remote.SetProtocol(ObfsFactory.GetObfs(server.protocol)); remote.SetObfs(ObfsFactory.GetObfs(server.obfs)); } if (connectionUDP != null && !server.udp_over_tcp) { try { remoteUDP = new ProxyEncryptSocket(ipAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); remoteUDP.GetSocket().Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0)); remoteUDP.CreateEncryptor(server.method, server.password); remoteUDP.SetProtocol(ObfsFactory.GetObfs(server.protocol)); remoteUDP.SetObfs(ObfsFactory.GetObfs(server.obfs)); if (server.server_udp_port == 0 || cfg.socks5RemotePort != 0) { IPEndPoint _remoteEP = new IPEndPoint(ipAddress, serverPort); remoteUDP.SetUdpEndPoint(_remoteEP); } else { IPEndPoint _remoteEP = new IPEndPoint(ipAddress, server.server_udp_port); remoteUDP.SetUdpEndPoint(_remoteEP); } } catch (SocketException) { remoteUDP = null; } } ResetTimeout(cfg.TTL); // Connect to the remote endpoint. if (cfg.socks5RemotePort == 0 && connectionUDP != null && !server.udp_over_tcp) { ConnectState _state = this.State; if (_state == ConnectState.CONNECTING) { StartPipe(); } } else { speedTester.BeginConnect(); IAsyncResult result = remote.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), new CallbackStatus()); double t = cfg.connect_timeout <= 0 ? 30 : cfg.connect_timeout; bool success = result.AsyncWaitHandle.WaitOne((int)(t * 1000), true); if (!success) { ((CallbackStatus)result.AsyncState).SetIfEqu(-1, 0); if (((CallbackStatus)result.AsyncState).Status == -1) { if (lastErrCode == 0) { lastErrCode = 8; server.ServerSpeedLog().AddTimeoutTimes(); } CloseSocket(ref remote); Close(); } } } } public bool TryReconnect() { if (local_error) return false; if (cfg.reconnectTimesRemain > 0) { if (this.State == ConnectState.CONNECTING) { return ReConnect(); } else if (this.State == ConnectState.CONNECTED && lastErrCode == 8) { if (connectionSendBufferList != null) { return ReConnect(); } } } return false; } private void CloseSocket(ref Socket sock) { lock (this) { if (sock != null) { Socket s = sock; sock = null; try { s.Shutdown(SocketShutdown.Both); } catch { } try { s.Close(); } catch { } } } } private void CloseSocket(ref ProxySocketTunLocal sock) { lock (this) { if (sock != null) { ProxySocketTunLocal s = sock; sock = null; try { s.Shutdown(SocketShutdown.Both); } catch { } try { s.Close(); } catch { } } } } private void CloseSocket(ref ProxyEncryptSocket sock) { lock (this) { if (sock != null) { ProxyEncryptSocket s = sock; sock = null; try { s.Shutdown(SocketShutdown.Both); } catch { } try { s.Close(); } catch { } } } } public override void Shutdown() { InvokeHandler handler = () => Close(); handler.BeginInvoke(null, null); } public void Close() { lock (this) { if (closed) { return; } closed = true; } Thread.Sleep(200); CloseSocket(ref remote); CloseSocket(ref remoteUDP); if (connection != null && cfg != null && connection.GetSocket() != null) { Logging.Debug("Close " + cfg.targetHost + ":" + cfg.targetPort.ToString() + " " + connection.GetSocket().Handle.ToString()); } if (lastErrCode == 0 && server != null && speedTester != null) { if (!local_error && speedTester.sizeProtocolRecv == 0 && speedTester.sizeUpload > 0) { if (is_protocol_sendback || (is_obfs_sendback && speedTester.sizeDownload == 0)) { lastErrCode = 16; server.ServerSpeedLog().AddErrorDecodeTimes(); } else server.ServerSpeedLog().AddErrorEmptyTimes(); } else server.ServerSpeedLog().AddNoErrorTimes(); } if (lastErrCode == 0 && server != null && cfg != null && keepCurrentServer != null) keepCurrentServer(localPort, cfg.targetHost, server.id); ResetTimeout(0); try { bool reconnect = TryReconnect(); //lock (this) { if (this.State != ConnectState.END) { if (this.State != ConnectState.READY && this.State != ConnectState.HANDSHAKE && server != null) { if (server.GetConnections().DecRef(this)) { server.ServerSpeedLog().AddDisconnectTimes(); } } this.State = ConnectState.END; } } if (!reconnect) { Logging.Info($"Disconnect {cfg.targetHost}:{cfg.targetPort.ToString()}"); CloseSocket(ref connection); CloseSocket(ref connectionUDP); Logging.Debug($"Transfer {cfg.targetHost}:{cfg.targetPort.ToString() + speedTester.TransferLog()}"); } else { connection = null; connectionUDP = null; } } catch (Exception e) { Logging.LogUsefulException(e); } getCurrentServer = null; keepCurrentServer = null; detector = null; speedTester = null; random = null; remoteUDPRecvBuffer = null; server = null; select_server = null; cfg = null; } private bool ConnectProxyServer(string strRemoteHost, int iRemotePort) { if (cfg.proxyType == 0) { bool ret = remote.ConnectSocks5ProxyServer(strRemoteHost, iRemotePort, connectionUDP != null && !server.udp_over_tcp, cfg.socks5RemoteUsername, cfg.socks5RemotePassword); remote.SetTcpServer(server.server, server.server_port); remote.SetUdpServer(server.server, server.server_udp_port == 0 ? server.server_port : server.server_udp_port); if (remoteUDP != null) { remoteUDP.GoS5Proxy = true; remoteUDP.SetUdpServer(server.server, server.server_udp_port == 0 ? server.server_port : server.server_udp_port); remoteUDP.SetUdpEndPoint(remote.GetProxyUdpEndPoint()); } return ret; } else if (cfg.proxyType == 1) { bool ret = remote.ConnectHttpProxyServer(strRemoteHost, iRemotePort, cfg.socks5RemoteUsername, cfg.socks5RemotePassword, cfg.proxyUserAgent); remote.SetTcpServer(server.server, server.server_port); return ret; } else { return true; } } private void Connect() { remote = null; remoteUDP = null; localPort = ((IPEndPoint)connection.GetSocket().LocalEndPoint).Port; if (select_server == null) { if (cfg.targetHost == null) { cfg.targetHost = GetQueryString(); cfg.targetPort = GetQueryPort(); server = this.getCurrentServer(localPort, null, cfg.targetHost, cfg.random, true); } else { server = this.getCurrentServer(localPort, null, cfg.targetHost, cfg.random, true, cfg.forceRandom); } } else { if (cfg.targetHost == null) { cfg.targetHost = GetQueryString(); cfg.targetPort = GetQueryPort(); server = this.getCurrentServer(localPort, select_server, cfg.targetHost, true, true); } else { server = this.getCurrentServer(localPort, select_server, cfg.targetHost, true, true, cfg.forceRandom); } } speedTester.server = server.server; Logging.Info($"Connect {cfg.targetHost}:{cfg.targetPort.ToString()} via {server.server}:{server.server_port}"); ResetTimeout(cfg.TTL); if (cfg.targetHost != null) { IPAddress ipAddress; string host = cfg.targetHost; if (!IPAddress.TryParse(host, out ipAddress)) { ipAddress = Utils.DnsBuffer.Get(host); if (ipAddress == null) { if (host.IndexOf('.') >= 0) { ipAddress = Utils.QueryDns(host, cfg.dns_servers); } else { ipAddress = Utils.QueryDns(host, null); } } Logging.Info($"DNS nolock query {host} answer {ipAddress.ToString()}"); if (ipAddress != null) { Utils.DnsBuffer.Set(host, new IPAddress(ipAddress.GetAddressBytes())); Utils.DnsBuffer.Sweep(); } } if (ipAddress != null) { cfg.targetHost = ipAddress.ToString(); ResetTimeout(cfg.TTL); } else { //throw new SocketException((int)SocketError.HostNotFound); Close(); return; } } lock (this) { server.ServerSpeedLog().AddConnectTimes(); if (this.State == ConnectState.HANDSHAKE) { this.State = ConnectState.CONNECTING; } server.GetConnections().AddRef(this); } try { IPAddress ipAddress; string serverHost = server.server; int serverPort = server.server_port; if (cfg.socks5RemotePort > 0) { serverHost = cfg.socks5RemoteHost; serverPort = cfg.socks5RemotePort; } bool parsed = IPAddress.TryParse(serverHost, out ipAddress); if (!parsed) { if (server.ServerSpeedLog().ErrorContinurousTimes > 10) server.DnsBuffer().force_expired = true; if (server.DnsBuffer().isExpired(serverHost)) { bool dns_ok = false; { DnsBuffer buf = server.DnsBuffer(); if (Monitor.TryEnter(buf, buf.ip != null ? 100 : 1000000)) { if (buf.isExpired(serverHost)) { if (serverHost.IndexOf('.') >= 0) { ipAddress = Util.Utils.QueryDns(serverHost, cfg.local_dns_servers); } else { ipAddress = Utils.QueryDns(serverHost, null); } if (ipAddress != null) { buf.UpdateDns(serverHost, ipAddress); dns_ok = true; } } else { ipAddress = buf.ip; dns_ok = true; } Monitor.Exit(buf); } else { if (buf.ip != null) { ipAddress = buf.ip; dns_ok = true; } } } if (!dns_ok) { if (server.DnsBuffer().ip != null) { ipAddress = server.DnsBuffer().ip; } else { lastErrCode = 8; server.ServerSpeedLog().AddTimeoutTimes(); Close(); return; } } } else { ipAddress = server.DnsBuffer().ip; } } BeginConnect(ipAddress, serverPort); } catch (Exception e) { LogException(e); Close(); } } private void ConnectCallback(IAsyncResult ar) { if (ar != null && ar.AsyncState != null) { ((CallbackStatus)ar.AsyncState).SetIfEqu(1, 0); if (((CallbackStatus)ar.AsyncState).Status != 1) return; } try { remote.EndConnect(ar); if (cfg.socks5RemotePort > 0) { if (!ConnectProxyServer(server.server, server.server_port)) { throw new SocketException((int)SocketError.ConnectionReset); } } speedTester.EndConnect(); ConnectState _state = this.State; if (_state == ConnectState.CONNECTING) { StartPipe(); } } catch (Exception e) { LogExceptionAndClose(e); } } // do/end xxx tcp/udp Recv private void doConnectionTCPRecv() { if (connection != null && connectionTCPIdle) { connectionTCPIdle = false; int recv_size = remote == null ? RecvSize : remote.TcpMSS - remote.OverHead; byte[] buffer = new byte[recv_size]; connection.BeginReceive(buffer, recv_size, 0, new AsyncCallback(PipeConnectionReceiveCallback), null); } } private int endConnectionTCPRecv(IAsyncResult ar) { if (connection != null) { int bytesRead = connection.EndReceive(ar); connectionTCPIdle = true; return bytesRead; } return 0; } private void doConnectionUDPRecv() { if (connectionUDP != null && connectionUDPIdle) { connectionUDPIdle = false; const int BufferSize = 65536; IPEndPoint sender = new IPEndPoint(connectionUDP.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0); EndPoint tempEP = (EndPoint)sender; byte[] buffer = new byte[BufferSize]; connectionUDP.BeginReceiveFrom(buffer, 0, BufferSize, SocketFlags.None, ref tempEP, new AsyncCallback(PipeConnectionUDPReceiveCallback), buffer); } } private int endConnectionUDPRecv(IAsyncResult ar, ref EndPoint endPoint) { if (connectionUDP != null) { int bytesRead = connectionUDP.EndReceiveFrom(ar, ref endPoint); if (connectionUDPEndPoint == null) connectionUDPEndPoint = (IPEndPoint)endPoint; connectionUDPIdle = true; return bytesRead; } return 0; } private void doRemoteTCPRecv() { if (remote != null && remoteTCPIdle) { remoteTCPIdle = false; remote.BeginReceive(new byte[BufferSize], RecvSize, 0, new AsyncCallback(PipeRemoteReceiveCallback), null); } } private int endRemoteTCPRecv(IAsyncResult ar) { if (remote != null) { bool sendback; int bytesRead = remote.EndReceive(ar, out sendback); int bytesRecv = remote.GetAsyncResultSize(ar); server.ServerSpeedLog().AddDownloadBytes(bytesRecv, DateTime.Now, speedTester.AddDownloadSize(bytesRecv)); if (sendback) { RemoteSend(remoteUDPRecvBuffer, 0); doConnectionRecv(); } remoteTCPIdle = true; return bytesRead; } return 0; } private void doRemoteUDPRecv() { if (remoteUDP != null && remoteUDPIdle) { remoteUDPIdle = false; const int BufferSize = 65536; IPEndPoint sender = new IPEndPoint(remoteUDP.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0); EndPoint tempEP = (EndPoint)sender; remoteUDP.BeginReceiveFrom(new byte[BufferSize], BufferSize, SocketFlags.None, ref tempEP, new AsyncCallback(PipeRemoteUDPReceiveCallback), null); } } private int endRemoteUDPRecv(IAsyncResult ar, ref EndPoint endPoint) { if (remoteUDP != null) { int bytesRead = remoteUDP.EndReceiveFrom(ar, ref endPoint); remoteUDPIdle = true; return bytesRead; } return 0; } private void doConnectionRecv() { doConnectionTCPRecv(); doConnectionUDPRecv(); } private void SetObfsPlugin() { int head_len = 30; if (connectionSendBufferList != null && connectionSendBufferList.Count > 0) { head_len = ObfsBase.GetHeadSize(connectionSendBufferList[0], 30); } else { head_len = ObfsBase.GetHeadSize(remoteHeaderSendBuffer, 30); } if (remote != null) remote.SetObfsPlugin(server, head_len); if (remoteUDP != null) remoteUDP.SetObfsPlugin(server, head_len); } private string GetQueryString() { if (remoteHeaderSendBuffer == null) return null; if (remoteHeaderSendBuffer[0] == 1) { if (remoteHeaderSendBuffer.Length > 4) { byte[] addr = new byte[4]; Array.Copy(remoteHeaderSendBuffer, 1, addr, 0, 4); IPAddress ipAddress = new IPAddress(addr); return ipAddress.ToString(); } return null; } if (remoteHeaderSendBuffer[0] == 4) { if (remoteHeaderSendBuffer.Length > 16) { byte[] addr = new byte[16]; Array.Copy(remoteHeaderSendBuffer, 1, addr, 0, 16); IPAddress ipAddress = new IPAddress(addr); return ipAddress.ToString(); } return null; } if (remoteHeaderSendBuffer[0] == 3 && remoteHeaderSendBuffer.Length > 1) { if (remoteHeaderSendBuffer.Length > remoteHeaderSendBuffer[1] + 1) { string url = System.Text.Encoding.UTF8.GetString(remoteHeaderSendBuffer, 2, remoteHeaderSendBuffer[1]); return url; } } return null; } private int GetQueryPort() { if (remoteHeaderSendBuffer == null) return 0; if (remoteHeaderSendBuffer[0] == 1) { if (remoteHeaderSendBuffer.Length > 6) { int port = (remoteHeaderSendBuffer[5] << 8) | remoteHeaderSendBuffer[6]; return port; } return 0; } if (remoteHeaderSendBuffer[0] == 4) { if (remoteHeaderSendBuffer.Length > 18) { int port = (remoteHeaderSendBuffer[17] << 8) | remoteHeaderSendBuffer[18]; return port; } return 0; } if (remoteHeaderSendBuffer[0] == 3 && remoteHeaderSendBuffer.Length > 1) { if (remoteHeaderSendBuffer.Length > remoteHeaderSendBuffer[1] + 2) { int port = (remoteHeaderSendBuffer[remoteHeaderSendBuffer[1] + 2] << 8) | remoteHeaderSendBuffer[remoteHeaderSendBuffer[1] + 3]; return port; } } return 0; } // 2 sides connection start private void StartPipe() { try { // set mark connectionTCPIdle = true; connectionUDPIdle = true; remoteTCPIdle = true; remoteUDPIdle = true; closed = false; remoteUDPRecvBufferLength = 0; SetObfsPlugin(); ResetTimeout(cfg.connect_timeout); speedTester.BeginUpload(); // remote ready if (connectionUDP == null) // TCP { if (cfg.reconnectTimes > 0 || cfg.targetPort != 0) { RemoteSend(remoteHeaderSendBuffer, remoteHeaderSendBuffer.Length); remoteHeaderSendBuffer = null; } is_protocol_sendback = remote.isProtocolSendback; is_obfs_sendback = remote.isObfsSendback; } else // UDP { if (!server.udp_over_tcp && remoteUDP != null) { if (cfg.socks5RemotePort == 0) CloseSocket(ref remote); remoteHeaderSendBuffer = null; } else if (remoteHeaderSendBuffer != null) { RemoteSend(remoteHeaderSendBuffer, remoteHeaderSendBuffer.Length); remoteHeaderSendBuffer = null; } } this.State = ConnectState.CONNECTED; if (connection.local_sendback_protocol != null) { connection.Send(remoteUDPRecvBuffer, 0, 0); } // remote recv first doRemoteTCPRecv(); doRemoteUDPRecv(); doConnectionTCPRecv(); doConnectionUDPRecv(); } catch (Exception e) { LogExceptionAndClose(e); } } private void ConnectionSend(byte[] buffer, int bytesToSend) { if (connectionUDP == null) { connection.Send(buffer, bytesToSend, SocketFlags.None); doRemoteUDPRecv(); } else { connectionUDP.BeginSendTo(buffer, 0, bytesToSend, SocketFlags.None, connectionUDPEndPoint, new AsyncCallback(PipeConnectionUDPSendCallback), null); } } private void UDPoverTCPConnectionSend(byte[] send_buffer, int bytesToSend) { List buffer_list = new List(); lock (recvUDPoverTCPLock) { Util.Utils.SetArrayMinSize(ref remoteUDPRecvBuffer, bytesToSend + remoteUDPRecvBufferLength); Array.Copy(send_buffer, 0, remoteUDPRecvBuffer, remoteUDPRecvBufferLength, bytesToSend); remoteUDPRecvBufferLength += bytesToSend; while (remoteUDPRecvBufferLength > 6) { int len = (remoteUDPRecvBuffer[0] << 8) + remoteUDPRecvBuffer[1]; if (len > remoteUDPRecvBufferLength) break; byte[] buffer = new byte[len]; Array.Copy(remoteUDPRecvBuffer, buffer, len); remoteUDPRecvBufferLength -= len; Array.Copy(remoteUDPRecvBuffer, len, remoteUDPRecvBuffer, 0, remoteUDPRecvBufferLength); buffer[0] = 0; buffer[1] = 0; buffer_list.Add(buffer); } } if (buffer_list.Count == 0) { doRemoteTCPRecv(); } else { foreach (byte[] buffer in buffer_list) { if (buffer == buffer_list[buffer_list.Count - 1]) connectionUDP.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, connectionUDPEndPoint, new AsyncCallback(PipeConnectionUDPSendCallback), null); else connectionUDP.BeginSendTo(buffer, 0, buffer.Length, SocketFlags.None, connectionUDPEndPoint, new AsyncCallback(PipeConnectionUDPSendCallbackNoRecv), null); } } } private void PipeRemoteReceiveCallback(IAsyncResult ar) { bool final_close = false; try { if (closed) { return; } int bytesRead = endRemoteTCPRecv(ar); if (remote.IsClose) { final_close = true; } else { int bytesRecv = remote.GetAsyncResultSize(ar); if (speedTester.BeginDownload()) { int pingTime = -1; if (speedTester.timeBeginDownload != null && speedTester.timeBeginUpload != null) pingTime = (int)(speedTester.timeBeginDownload - speedTester.timeBeginUpload).TotalMilliseconds; if (pingTime >= 0) server.ServerSpeedLog().AddConnectTime(pingTime); } ResetTimeout(cfg.TTL); speedTester.AddProtocolRecvSize(remote.GetAsyncProtocolSize(ar)); if (bytesRead > 0) { byte[] remoteSendBuffer = new byte[BufferSize]; Array.Copy(remote.GetAsyncResultBuffer(ar), remoteSendBuffer, bytesRead); if (connectionUDP == null) { if (detector.OnRecv(remoteSendBuffer, bytesRead) > 0) { server.ServerSpeedLog().AddErrorTimes(); } if (detector.Pass) { server.ServerSpeedLog().ResetErrorDecodeTimes(); } else { server.ServerSpeedLog().ResetEmptyTimes(); } connection.Send(remoteSendBuffer, bytesRead, 0); } else { UDPoverTCPConnectionSend(remoteSendBuffer, bytesRead); } server.ServerSpeedLog().AddDownloadRawBytes(bytesRead); speedTester.AddRecvSize(bytesRead); _totalRecvSize += bytesRead; } if (connectionUDP == null && _totalRecvSize > 1024 * 1024 * 2) { PipeRemoteReceiveLoop(); } else { doRemoteTCPRecv(); } } } catch (Exception e) { LogException(e); final_close = true; } finally { if (final_close) { Close(); } } } private void PipeRemoteReceiveLoop() { bool final_close = false; byte[] recv_buffer = new byte[BufferSize * 4]; DateTime beforeReceive = DateTime.Now; while (!closed) { try { int protocolSize; bool sendback; int bytesRecv; int bytesRead = remote.Receive(recv_buffer, RecvSize, 0, out bytesRecv, out protocolSize, out sendback); DateTime now = DateTime.Now; if (remote != null && remote.IsClose) { final_close = true; break; } if (closed) { break; } if (speedTester.BeginDownload()) { int pingTime = -1; if (speedTester.timeBeginDownload != null && speedTester.timeBeginUpload != null) pingTime = (int)(speedTester.timeBeginDownload - speedTester.timeBeginUpload).TotalMilliseconds; if (pingTime >= 0) server.ServerSpeedLog().AddConnectTime(pingTime); } server.ServerSpeedLog().AddDownloadBytes(bytesRecv, now, speedTester.AddDownloadSize(bytesRecv)); ResetTimeout(cfg.TTL); if (sendback) { RemoteSend(remoteUDPRecvBuffer, 0); doConnectionRecv(); } if (bytesRead > 0) { byte[] remoteSendBuffer = new byte[BufferSize]; Array.Copy(recv_buffer, remoteSendBuffer, bytesRead); if (connectionUDP == null) { if (detector.OnRecv(remoteSendBuffer, bytesRead) > 0) { server.ServerSpeedLog().AddErrorTimes(); } if (detector.Pass) { server.ServerSpeedLog().ResetErrorDecodeTimes(); } else { server.ServerSpeedLog().ResetEmptyTimes(); } connection.Send(remoteSendBuffer, bytesRead, 0); } else { UDPoverTCPConnectionSend(remoteSendBuffer, bytesRead); } speedTester.AddProtocolRecvSize(protocolSize); server.ServerSpeedLog().AddDownloadRawBytes(bytesRead); speedTester.AddRecvSize(bytesRead); } if ((now - beforeReceive).TotalSeconds > 5) { _totalRecvSize = 0; doRemoteTCPRecv(); return; } else { beforeReceive = now; } } catch (Exception e) { LogException(e); final_close = true; break; } } if (final_close) Close(); } // end ReceiveCallback private void PipeRemoteUDPReceiveCallback(IAsyncResult ar) { bool final_close = false; try { if (closed) { return; } IPEndPoint sender = new IPEndPoint(remoteUDP.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0); EndPoint tempEP = (EndPoint)sender; int bytesRead = endRemoteUDPRecv(ar, ref tempEP); if (remoteUDP.IsClose) { final_close = true; } else { int bytesRecv = remoteUDP.GetAsyncResultSize(ar); if (speedTester.BeginDownload()) { int pingTime = -1; if (speedTester.timeBeginDownload != null && speedTester.timeBeginUpload != null) pingTime = (int)(speedTester.timeBeginDownload - speedTester.timeBeginUpload).TotalMilliseconds; if (pingTime >= 0) server.ServerSpeedLog().AddConnectTime(pingTime); } server.ServerSpeedLog().AddDownloadBytes(bytesRecv, DateTime.Now, speedTester.AddDownloadSize(bytesRecv)); ResetTimeout(cfg.TTL); if (bytesRead <= 0) { doRemoteUDPRecv(); } else //if (bytesRead > 0) { ConnectionSend(remoteUDP.GetAsyncResultBuffer(ar), bytesRead); speedTester.AddRecvSize(bytesRead); server.ServerSpeedLog().AddDownloadRawBytes(bytesRead); } } } catch (Exception e) { LogException(e); final_close = true; } finally { if (final_close) { Close(); } } } private int RemoteSend(byte[] bytes, int length) { int total_len = 0; int send_len; send_len = remote.Send(bytes, length, SocketFlags.None); if (send_len > 0) { server.ServerSpeedLog().AddUploadBytes(send_len, DateTime.Now, speedTester.AddUploadSize(send_len)); if (length >= 0) ResetTimeout(cfg.TTL); else ResetTimeout(cfg.connect_timeout <= 0 ? 30 : cfg.connect_timeout, false); total_len += send_len; if (lastKeepTime == null || (DateTime.Now - lastKeepTime).TotalSeconds > 5) { if (keepCurrentServer != null) { keepCurrentServer(localPort, cfg.targetHost, server.id); } lastKeepTime = DateTime.Now; } while (true) { send_len = remote.Send(null, 0, SocketFlags.None); if (send_len > 0) { server.ServerSpeedLog().AddUploadBytes(send_len, DateTime.Now, speedTester.AddUploadSize(send_len)); total_len += send_len; } else break; } } return total_len; } private void RemoteSendto(byte[] bytes, int length) { int send_len; send_len = remoteUDP.BeginSendTo(bytes, length, SocketFlags.None, new AsyncCallback(PipeRemoteUDPSendCallback), null); server.ServerSpeedLog().AddUploadBytes(send_len, DateTime.Now, speedTester.AddUploadSize(send_len)); } private void PipeConnectionReceiveCallback(IAsyncResult ar) { bool final_close = false; try { if (closed) { return; } int bytesRead = endConnectionTCPRecv(ar); if (bytesRead > 0) { if (connectionUDP != null) { doConnectionTCPRecv(); ResetTimeout(cfg.TTL); return; } byte[] connetionRecvBuffer = new byte[BufferSize]; Array.Copy((ar.AsyncState as CallbackState).buffer, 0, connetionRecvBuffer, 0, bytesRead); if (connectionSendBufferList != null) { detector.OnSend(connetionRecvBuffer, bytesRead); byte[] data = new byte[bytesRead]; Array.Copy(connetionRecvBuffer, data, data.Length); connectionSendBufferList.Add(data); } if (State == ConnectState.CONNECTED) { if (remoteHeaderSendBuffer != null) { Array.Copy(connetionRecvBuffer, 0, connetionRecvBuffer, remoteHeaderSendBuffer.Length, bytesRead); Array.Copy(remoteHeaderSendBuffer, 0, connetionRecvBuffer, 0, remoteHeaderSendBuffer.Length); bytesRead += remoteHeaderSendBuffer.Length; remoteHeaderSendBuffer = null; } else { Logging.LogBin(LogLevel.Debug, "remote send", connetionRecvBuffer, bytesRead); } } if (speedTester.sizeRecv > 0) { connectionSendBufferList = null; server.ServerSpeedLog().ResetContinurousTimes(); } if (closed || State != ConnectState.CONNECTED) { return; } if (connectionSendBufferList != null) { ResetTimeout(cfg.connect_timeout); } else { ResetTimeout(cfg.TTL); } int send_len = RemoteSend(connetionRecvBuffer, bytesRead); if (!( send_len == 0 && bytesRead > 0) ) doConnectionRecv(); } else { local_error = true; final_close = true; } } catch (Exception e) { local_error = true; LogException(e); final_close = true; } finally { if (final_close) { Close(); } } } private void PipeConnectionUDPReceiveCallback(IAsyncResult ar) { bool final_close = false; try { if (closed) { return; } IPEndPoint sender = new IPEndPoint(connectionUDP.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0); EndPoint tempEP = (EndPoint)sender; int bytesRead = endConnectionUDPRecv(ar, ref tempEP); if (bytesRead > 0) { byte[] connetionSendBuffer = new byte[bytesRead]; Array.Copy((byte[])ar.AsyncState, connetionSendBuffer, bytesRead); if (!server.udp_over_tcp && remoteUDP != null) { RemoteSendto(connetionSendBuffer, bytesRead); } else { if (connetionSendBuffer[0] == 0 && connetionSendBuffer[1] == 0) { connetionSendBuffer[0] = (byte)(bytesRead >> 8); connetionSendBuffer[1] = (byte)(bytesRead); RemoteSend(connetionSendBuffer, bytesRead); doConnectionRecv(); } } ResetTimeout(cfg.TTL); } else { final_close = true; } } catch (Exception e) { LogException(e); final_close = true; } finally { if (final_close) { Close(); } } } private void PipeRemoteUDPSendCallback(IAsyncResult ar) { if (closed) { return; } try { remoteUDP.EndSendTo(ar); doConnectionRecv(); } catch (Exception e) { LogExceptionAndClose(e); } } private void PipeConnectionUDPSendCallbackNoRecv(IAsyncResult ar) { if (closed) { return; } try { connectionUDP.EndSendTo(ar); } catch (Exception e) { LogExceptionAndClose(e); } } private void PipeConnectionUDPSendCallback(IAsyncResult ar) { if (closed) { return; } try { connectionUDP.EndSendTo(ar); doRemoteTCPRecv(); doRemoteUDPRecv(); } catch (Exception e) { LogExceptionAndClose(e); } } protected string getServerUrl(out string remarks) { Server s = server; if (s == null) { remarks = ""; return ""; } remarks = s.remarks; return s.server; } private void LogException(Exception e) { int err = LogSocketException(e); string remarks; string server_url = getServerUrl(out remarks); if (err != 0 && !Logging.LogSocketException(remarks, server_url, e)) Logging.LogUsefulException(e); } private void LogExceptionAndClose(Exception e) { LogException(e); Close(); } } } ================================================ FILE: shadowsocks-csharp/Controller/Logging.cs ================================================ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net.Sockets; using System.Text; using Shadowsocks.Obfs; namespace Shadowsocks.Controller { public enum LogLevel { Debug = 0, Info, Warn, Error, Assert, } public class Logging { public static string LogFile; public static string LogFilePath; public static string LogFileName; protected static string date; private static FileStream _logFileStream; private static StreamWriterWithTimestamp _logStreamWriter; private static object _lock = new object(); public static bool save_to_file = true; public static bool OpenLogFile() { try { CloseLogFile(); if (save_to_file) { string curpath = Path.Combine(System.Windows.Forms.Application.StartupPath, @"temp");// Path.GetFullPath(".");//Path.GetTempPath(); LogFilePath = curpath; if (!Directory.Exists(curpath)) { Directory.CreateDirectory(curpath); } string new_date = DateTime.Now.ToString("yyyy-MM"); LogFileName = "shadowsocks_" + new_date + ".log"; LogFile = Path.Combine(curpath, LogFileName); _logFileStream = new FileStream(LogFile, FileMode.Append); _logStreamWriter = new StreamWriterWithTimestamp(_logFileStream); _logStreamWriter.AutoFlush = true; Console.SetOut(_logStreamWriter); Console.SetError(_logStreamWriter); date = new_date; } else { Console.SetOut(Console.Out); Console.SetError(Console.Error); } return true; } catch (IOException e) { Console.WriteLine(e.ToString()); return false; } } private static void CloseLogFile() { _logStreamWriter?.Dispose(); _logFileStream?.Dispose(); _logStreamWriter = null; _logFileStream = null; } public static void Clear() { CloseLogFile(); if (LogFile != null) { File.Delete(LogFile); } OpenLogFile(); } public static void Error(object o) { Log(LogLevel.Error, o); System.Diagnostics.Debug.WriteLine($@"[{DateTime.Now}] ERROR {o}"); } public static void Info(object o) { Log(LogLevel.Info, o); System.Diagnostics.Debug.WriteLine($@"[{DateTime.Now}] INFO {o}"); } [Conditional("DEBUG")] public static void Debug(object o) { Log(LogLevel.Debug, o); System.Diagnostics.Debug.WriteLine($@"[{DateTime.Now}] DEBUG {o}"); } private static string ToString(StackFrame[] stacks) { string result = string.Empty; foreach (StackFrame stack in stacks) { result += string.Format("{0}\r\n", stack.GetMethod().ToString()); } return result; } protected static void UpdateLogFile() { if (DateTime.Now.ToString("yyyy-MM") != date) { lock (_lock) { if (DateTime.Now.ToString("yyyy-MM") != date) { OpenLogFile(); } } } } public static void LogUsefulException(Exception e) { UpdateLogFile(); // just log useful exceptions, not all of them if (e is SocketException) { SocketException se = (SocketException)e; if (se.SocketErrorCode == SocketError.ConnectionAborted) { // closed by browser when sending // normally happens when download is canceled or a tab is closed before page is loaded } else if (se.SocketErrorCode == SocketError.ConnectionReset) { // received rst } else if (se.SocketErrorCode == SocketError.NotConnected) { // close when not connected } else if ((uint)se.SocketErrorCode == 0x80004005) { // already closed } else if (se.SocketErrorCode == SocketError.Shutdown) { // ignore } else if (se.SocketErrorCode == SocketError.Interrupted) { // ignore } else { Error(e); Debug(ToString(new StackTrace().GetFrames())); } } else { Error(e); Debug(ToString(new StackTrace().GetFrames())); } } public static bool LogSocketException(string remarks, string server, Exception e) { UpdateLogFile(); // just log useful exceptions, not all of them if (e is ObfsException) { ObfsException oe = (ObfsException)e; Error("Proxy server [" + remarks + "(" + server + ")] " + oe.Message); return true; } else if (e is NullReferenceException) { return true; } else if (e is ObjectDisposedException) { // ignore return true; } else if (e is SocketException) { SocketException se = (SocketException)e; if ((uint)se.SocketErrorCode == 0x80004005) { // already closed return true; } else if (se.ErrorCode == 11004) { Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] " + "DNS lookup failed"); return true; } else if (se.SocketErrorCode == SocketError.HostNotFound) { Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] " + "Host not found"); return true; } else if (se.SocketErrorCode == SocketError.ConnectionRefused) { Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] " + "connection refused"); return true; } else if (se.SocketErrorCode == SocketError.NetworkUnreachable) { Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] " + "network unreachable"); return true; } else if (se.SocketErrorCode == SocketError.TimedOut) { //Logging.Log(LogLevel.Warn, "Proxy server [" + remarks + "(" + server + ")] " // + "connection timeout"); return true; } else if (se.SocketErrorCode == SocketError.Shutdown) { return true; } else { Logging.Log(LogLevel.Info, "Proxy server [" + remarks + "(" + server + ")] " + Convert.ToString(se.SocketErrorCode) + ":" + se.Message); Debug(ToString(new StackTrace().GetFrames())); return true; } } return false; } public static void Log(LogLevel level, object s) { UpdateLogFile(); var strMap = new []{ "Debug", "Info", "Warn", "Error", "Assert", }; Console.WriteLine($@"[{strMap[(int)level]}] {s}"); } [Conditional("DEBUG")] public static void LogBin(LogLevel level, string info, byte[] data, int length) { //string s = ""; //for (int i = 0; i < length; ++i) //{ // string fs = "0" + Convert.ToString(data[i], 16); // s += " " + fs.Substring(fs.Length - 2, 2); //} //Log(level, info + s); } } // Simply extended System.IO.StreamWriter for adding timestamp workaround public class StreamWriterWithTimestamp : StreamWriter { public StreamWriterWithTimestamp(Stream stream) : base(stream) { } private string GetTimestamp() { return "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] "; } public override void WriteLine(string value) { base.WriteLine(GetTimestamp() + value); } public override void Write(string value) { base.Write(GetTimestamp() + value); } } } ================================================ FILE: shadowsocks-csharp/Controller/PACServer.cs ================================================ 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"; public static string USER_ABP_FILE = "abp.txt"; FileSystemWatcher PACFileWatcher; FileSystemWatcher UserRuleFileWatcher; private Configuration _config; public event EventHandler PACFileChanged; public event EventHandler UserRuleFileChanged; public PACServer() { this.WatchPacFile(); this.WatchUserRuleFile(); } 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(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); bool hostMatch = false, pathMatch = false; int socksType = 0; string proxy = null; 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 (!Util.Utils.isLocal(socket) || line.IndexOf("auth=" + _config.localAuthPassword) > 0) { if (line.IndexOf(" /pac?") > 0 && line.IndexOf("GET") == 0) { string url = line.Substring(line.IndexOf(" ") + 1); url = url.Substring(0, url.IndexOf(" ")); pathMatch = true; int port_pos = url.IndexOf("port="); if (port_pos > 0) { string port = url.Substring(port_pos + 5); if (port.IndexOf("&") >= 0) { port = port.Substring(0, port.IndexOf("&")); } int ip_pos = url.IndexOf("ip="); if (ip_pos > 0) { proxy = url.Substring(ip_pos + 3); if (proxy.IndexOf("&") >= 0) { proxy = proxy.Substring(0, proxy.IndexOf("&")); } proxy += ":" + port + ";"; } else { proxy = "127.0.0.1:" + port + ";"; } } if (url.IndexOf("type=socks4") > 0 || url.IndexOf("type=s4") > 0) { socksType = 4; } if (url.IndexOf("type=socks5") > 0 || url.IndexOf("type=s5") > 0) { socksType = 5; } } } } } if (hostMatch && pathMatch) { SendResponse(firstPacket, length, socket, socksType, proxy); 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, int socksType, string setProxy) { try { string pac = GetPACContent(); IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; string proxy = setProxy == null ? GetPACAddress(firstPacket, length, localEndPoint, socksType) : socksType == 5 ? "SOCKS5 " + setProxy : socksType == 4 ? "SOCKS " + setProxy : "PROXY " + setProxy; if (_config.pacDirectGoProxy && _config.proxyEnable) { if (_config.proxyType == 0) pac = pac.Replace("__DIRECT__", "SOCKS5 " + _config.proxyHost + ":" + _config.proxyPort.ToString() + ";DIRECT;"); else if (_config.proxyType == 1) pac = pac.Replace("__DIRECT__", "PROXY " + _config.proxyHost + ":" + _config.proxyPort.ToString() + ";DIRECT;"); } else pac = pac.Replace("__DIRECT__", "DIRECT;"); pac = pac.Replace("__PROXY__", proxy + "DIRECT;"); string text = String.Format(@"HTTP/1.1 200 OK Server: ShadowsocksR 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); } catch (Exception e) { Console.WriteLine(e); socket.Shutdown(SocketShutdown.Both); socket.Close(); } } private void SendCallback(IAsyncResult ar) { Socket conn = (Socket)ar.AsyncState; try { conn.Shutdown(SocketShutdown.Both); conn.Close(); } catch { } } private void WatchPacFile() { if (PACFileWatcher != null) { PACFileWatcher.Dispose(); } PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; PACFileWatcher.Filter = PAC_FILE; PACFileWatcher.Changed += Watcher_Changed; PACFileWatcher.Created += Watcher_Changed; PACFileWatcher.Deleted += Watcher_Changed; PACFileWatcher.Renamed += Watcher_Changed; PACFileWatcher.EnableRaisingEvents = true; } private void WatchUserRuleFile() { if (UserRuleFileWatcher != null) { UserRuleFileWatcher.Dispose(); } UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory()); UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; UserRuleFileWatcher.Filter = USER_RULE_FILE; UserRuleFileWatcher.Changed += UserRuleFileWatcher_Changed; UserRuleFileWatcher.Created += UserRuleFileWatcher_Changed; UserRuleFileWatcher.Deleted += UserRuleFileWatcher_Changed; UserRuleFileWatcher.Renamed += UserRuleFileWatcher_Changed; UserRuleFileWatcher.EnableRaisingEvents = true; } private void Watcher_Changed(object sender, FileSystemEventArgs e) { if (PACFileChanged != null) { PACFileChanged(this, new EventArgs()); } } private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e) { if (UserRuleFileChanged != null) { UserRuleFileChanged(this, new EventArgs()); } } private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, int socksType) { //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); //} if (socksType == 5) { return "SOCKS5 " + localEndPoint.Address + ":" + this._config.localPort + ";"; } else if (socksType == 4) { return "SOCKS " + localEndPoint.Address + ":" + this._config.localPort + ";"; } return "PROXY " + localEndPoint.Address + ":" + this._config.localPort + ";"; } } } ================================================ FILE: shadowsocks-csharp/Controller/ProxyAuth.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using Shadowsocks.Encryption; using Shadowsocks.Obfs; using Shadowsocks.Model; using System.Timers; using System.Threading; namespace Shadowsocks.Controller { public class ProtocolException : Exception { public ProtocolException(string info) : base(info) { } } class ProxyAuthHandler { private Configuration _config; private ServerTransferTotal _transfer; private IPRangeSet _IPRange; private byte[] _firstPacket; private int _firstPacketLength; private Socket _connection; private Socket _connectionUDP; private string local_sendback_protocol; protected const int RECV_SIZE = 16384; protected byte[] _connetionRecvBuffer = new byte[RECV_SIZE * 2]; public byte command; protected byte[] _remoteHeaderSendBuffer; protected HttpPraser httpProxyState; public ProxyAuthHandler(Configuration config, ServerTransferTotal transfer, IPRangeSet IPRange, byte[] firstPacket, int length, Socket socket) { int local_port = ((IPEndPoint)socket.LocalEndPoint).Port; _config = config; _transfer = transfer; _IPRange = IPRange; _firstPacket = firstPacket; _firstPacketLength = length; _connection = socket; socket.NoDelay = true; if (_config.GetPortMapCache().ContainsKey(local_port) && _config.GetPortMapCache()[local_port].type == PortMapType.Forward) { Connect(); } else { HandshakeReceive(); } } private void CloseSocket(ref Socket sock) { lock (this) { if (sock != null) { Socket s = sock; sock = null; try { s.Shutdown(SocketShutdown.Both); } catch { } try { s.Close(); } catch { } } } } private void Close() { CloseSocket(ref _connection); CloseSocket(ref _connectionUDP); _config = null; } bool AuthConnection(Socket connection, string authUser, string authPass) { if ((_config.authUser ?? "").Length == 0) { return true; } if (_config.authUser == authUser && (_config.authPass ?? "") == authPass) { return true; } if (Util.Utils.isMatchSubNet(((IPEndPoint)_connection.RemoteEndPoint).Address, "127.0.0.0/8")) { return true; } return false; } private void HandshakeReceive() { try { int bytesRead = _firstPacketLength; if (bytesRead > 1) { if ((!string.IsNullOrEmpty(_config.authUser) || Util.Utils.isMatchSubNet(((IPEndPoint)_connection.RemoteEndPoint).Address, "127.0.0.0/8")) && _firstPacket[0] == 4 && _firstPacketLength >= 9) { RspSocks4aHandshakeReceive(); } else if (_firstPacket[0] == 5 && _firstPacketLength >= 3) { RspSocks5HandshakeReceive(); } else { RspHttpHandshakeReceive(); } } else { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void RspSocks4aHandshakeReceive() { List firstPacket = new List(); for (int i = 0; i < _firstPacketLength; ++i) { firstPacket.Add(_firstPacket[i]); } List dataSockSend = firstPacket.GetRange(0, 4); dataSockSend[0] = 0; dataSockSend[1] = 90; bool remoteDNS = (_firstPacket[4] == 0 && _firstPacket[5] == 0 && _firstPacket[6] == 0 && _firstPacket[7] == 1) ? true : false; if (remoteDNS) { for (int i = 0; i < 4; ++i) { dataSockSend.Add(0); } int addrStartPos = firstPacket.IndexOf(0x0, 8); List addr = firstPacket.GetRange(addrStartPos + 1, firstPacket.Count - addrStartPos - 2); _remoteHeaderSendBuffer = new byte[2 + addr.Count + 2]; _remoteHeaderSendBuffer[0] = 3; _remoteHeaderSendBuffer[1] = (byte)addr.Count; Array.Copy(addr.ToArray(), 0, _remoteHeaderSendBuffer, 2, addr.Count); _remoteHeaderSendBuffer[2 + addr.Count] = dataSockSend[2]; _remoteHeaderSendBuffer[2 + addr.Count + 1] = dataSockSend[3]; } else { for (int i = 0; i < 4; ++i) { dataSockSend.Add(_firstPacket[4 + i]); } _remoteHeaderSendBuffer = new byte[1 + 4 + 2]; _remoteHeaderSendBuffer[0] = 1; Array.Copy(dataSockSend.ToArray(), 4, _remoteHeaderSendBuffer, 1, 4); _remoteHeaderSendBuffer[1 + 4] = dataSockSend[2]; _remoteHeaderSendBuffer[1 + 4 + 1] = dataSockSend[3]; } command = 1; // Set TCP connect command _connection.Send(dataSockSend.ToArray()); Connect(); } private void RspSocks5HandshakeReceive() { byte[] response = { 5, 0 }; if (_firstPacket[0] != 5) { response = new byte[] { 0, 91 }; Console.WriteLine("socks 4/5 protocol error"); _connection.Send(response); Close(); return; } bool no_auth = false; bool auth = false; bool has_method = false; for (int index = 0; index < _firstPacket[1]; ++index) { if (_firstPacket[2 + index] == 0) { no_auth = true; has_method = true; } else if (_firstPacket[2 + index] == 2) { auth = true; has_method = true; } } if (!has_method) { Console.WriteLine("Socks5 no acceptable auth method"); Close(); return; } if (auth || !no_auth) { response[1] = 2; _connection.Send(response); HandshakeAuthReceiveCallback(); } else if (no_auth && (string.IsNullOrEmpty(_config.authUser) || Util.Utils.isMatchSubNet(((IPEndPoint)_connection.RemoteEndPoint).Address, "127.0.0.0/8"))) { _connection.Send(response); HandshakeReceive2Callback(); } else { Console.WriteLine("Socks5 Auth failed"); Close(); } } private void HandshakeAuthReceiveCallback() { try { int bytesRead = _connection.Receive(_connetionRecvBuffer, 1024, 0); //_connection.EndReceive(ar); if (bytesRead >= 3) { byte user_len = _connetionRecvBuffer[1]; byte pass_len = _connetionRecvBuffer[user_len + 2]; byte[] response = { 1, 0 }; string user = Encoding.UTF8.GetString(_connetionRecvBuffer, 2, user_len); string pass = Encoding.UTF8.GetString(_connetionRecvBuffer, user_len + 3, pass_len); if (AuthConnection(_connection, user, pass)) { _connection.Send(response); HandshakeReceive2Callback(); } } else { Console.WriteLine("failed to recv data in HandshakeAuthReceiveCallback"); Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void HandshakeReceive2Callback() { try { // +----+-----+-------+------+----------+----------+ // |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT | // +----+-----+-------+------+----------+----------+ // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ int bytesRead = _connection.Receive(_connetionRecvBuffer, 5, 0); if (bytesRead >= 5) { command = _connetionRecvBuffer[1]; _remoteHeaderSendBuffer = new byte[bytesRead - 3]; Array.Copy(_connetionRecvBuffer, 3, _remoteHeaderSendBuffer, 0, _remoteHeaderSendBuffer.Length); int recv_size = 0; if (_remoteHeaderSendBuffer[0] == 1) recv_size = 4 - 1; else if (_remoteHeaderSendBuffer[0] == 4) recv_size = 16 - 1; else if (_remoteHeaderSendBuffer[0] == 3) recv_size = _remoteHeaderSendBuffer[1]; if (recv_size == 0) throw new Exception("Wrong socks5 addr type"); HandshakeReceive3Callback(recv_size + 2); // recv port } else { Console.WriteLine("failed to recv data in HandshakeReceive2Callback"); Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void HandshakeReceive3Callback(int recv_size) { try { int bytesRead = _connection.Receive(_connetionRecvBuffer, recv_size, 0); if (bytesRead > 0) { Array.Resize(ref _remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length + bytesRead); Array.Copy(_connetionRecvBuffer, 0, _remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length - bytesRead, bytesRead); if (command == 3) { RspSocks5UDPHeader(bytesRead); } else { //RspSocks5TCPHeader(); local_sendback_protocol = "socks5"; Connect(); } } else { Console.WriteLine("failed to recv data in HandshakeReceive3Callback"); Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void RspSocks5UDPHeader(int bytesRead) { bool ipv6 = _connection.AddressFamily == AddressFamily.InterNetworkV6; int udpPort = 0; if (bytesRead >= 3 + 6) { ipv6 = _remoteHeaderSendBuffer[0] == 4; if (!ipv6) udpPort = _remoteHeaderSendBuffer[5] * 0x100 + _remoteHeaderSendBuffer[6]; else udpPort = _remoteHeaderSendBuffer[17] * 0x100 + _remoteHeaderSendBuffer[18]; } if (!ipv6) { _remoteHeaderSendBuffer = new byte[1 + 4 + 2]; _remoteHeaderSendBuffer[0] = 0x8 | 1; _remoteHeaderSendBuffer[5] = (byte)(udpPort / 0x100); _remoteHeaderSendBuffer[6] = (byte)(udpPort % 0x100); } else { _remoteHeaderSendBuffer = new byte[1 + 16 + 2]; _remoteHeaderSendBuffer[0] = 0x8 | 4; _remoteHeaderSendBuffer[17] = (byte)(udpPort / 0x100); _remoteHeaderSendBuffer[18] = (byte)(udpPort % 0x100); } int port = 0; IPAddress ip = ipv6 ? IPAddress.IPv6Any : IPAddress.Any; _connectionUDP = new Socket(ip.AddressFamily, SocketType.Dgram, ProtocolType.Udp); for (; port < 65536; ++port) { try { _connectionUDP.Bind(new IPEndPoint(ip, port)); break; } catch (Exception) { // } } port = ((IPEndPoint)_connectionUDP.LocalEndPoint).Port; if (!ipv6) { byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, (byte)(port / 0x100), (byte)(port % 0x100) }; byte[] ip_bytes = ((IPEndPoint)_connection.LocalEndPoint).Address.GetAddressBytes(); Array.Copy(ip_bytes, 0, response, 4, 4); _connection.Send(response); Connect(); } else { byte[] response = { 5, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte)(port / 0x100), (byte)(port % 0x100) }; byte[] ip_bytes = ((IPEndPoint)_connection.LocalEndPoint).Address.GetAddressBytes(); Array.Copy(ip_bytes, 0, response, 4, 16); _connection.Send(response); Connect(); } } private void RspSocks5TCPHeader() { if (_connection.AddressFamily == AddressFamily.InterNetwork) { byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 }; _connection.Send(response); } else { byte[] response = { 5, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; _connection.Send(response); } } private void RspHttpHandshakeReceive() { command = 1; // Set TCP connect command if (httpProxyState == null) { httpProxyState = new HttpPraser(); } if (Util.Utils.isMatchSubNet(((IPEndPoint)_connection.RemoteEndPoint).Address, "127.0.0.0/8")) { httpProxyState.httpAuthUser = ""; httpProxyState.httpAuthPass = ""; } else { httpProxyState.httpAuthUser = _config.authUser; httpProxyState.httpAuthPass = _config.authPass; } for (int i = 1; ; ++i) { int err = httpProxyState.HandshakeReceive(_firstPacket, _firstPacketLength, ref _remoteHeaderSendBuffer); if (err == 1) { if (HttpHandshakeRecv()) break; } else if (err == 2) { string dataSend = httpProxyState.Http407(); byte[] httpData = System.Text.Encoding.UTF8.GetBytes(dataSend); _connection.Send(httpData); if (HttpHandshakeRecv()) break; } else if (err == 3 || err == 4) { Connect(); break; } else if (err == 0) { local_sendback_protocol = "http"; Connect(); break; } else if (err == 500) { string dataSend = httpProxyState.Http500(); byte[] httpData = System.Text.Encoding.UTF8.GetBytes(dataSend); _connection.Send(httpData); if (HttpHandshakeRecv()) break; } if (i == 3) { Close(); break; } } } private bool HttpHandshakeRecv() { try { int bytesRead = _connection.Receive(_connetionRecvBuffer, _firstPacket.Length, 0); if (bytesRead > 0) { Array.Copy(_connetionRecvBuffer, _firstPacket, bytesRead); _firstPacketLength = bytesRead; return false; } else { Console.WriteLine("failed to recv data in HttpHandshakeRecv"); Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } return true; } private void Connect() { Handler.GetCurrentServer getCurrentServer = delegate (int localPort, ServerSelectStrategy.FilterFunc filter, string targetURI, bool cfgRandom, bool usingRandom, bool forceRandom) { return _config.GetCurrentServer(localPort, filter, targetURI, cfgRandom, usingRandom, forceRandom); }; Handler.KeepCurrentServer keepCurrentServer = delegate (int localPort, string targetURI, string id) { _config.KeepCurrentServer(localPort, targetURI, id); }; int local_port = ((IPEndPoint)_connection.LocalEndPoint).Port; Handler handler = new Handler(); handler.getCurrentServer = getCurrentServer; handler.keepCurrentServer = keepCurrentServer; handler.connection = new ProxySocketTunLocal(_connection); handler.connectionUDP = _connectionUDP; handler.cfg.reconnectTimesRemain = _config.reconnectTimes; handler.cfg.random = _config.random; handler.cfg.forceRandom = _config.random; handler.setServerTransferTotal(_transfer); if (_config.proxyEnable) { handler.cfg.proxyType = _config.proxyType; handler.cfg.socks5RemoteHost = _config.proxyHost; handler.cfg.socks5RemotePort = _config.proxyPort; handler.cfg.socks5RemoteUsername = _config.proxyAuthUser; handler.cfg.socks5RemotePassword = _config.proxyAuthPass; handler.cfg.proxyUserAgent = _config.proxyUserAgent; } handler.cfg.TTL = _config.TTL; handler.cfg.connect_timeout = _config.connectTimeout; handler.cfg.autoSwitchOff = _config.autoBan; if (!string.IsNullOrEmpty(_config.localDnsServer)) { handler.cfg.local_dns_servers = _config.localDnsServer; } if (!string.IsNullOrEmpty(_config.dnsServer)) { handler.cfg.dns_servers = _config.dnsServer; } if (_config.GetPortMapCache().ContainsKey(local_port)) { PortMapConfigCache cfg = _config.GetPortMapCache()[local_port]; if (cfg.server == null || cfg.id == cfg.server.id) { if (cfg.server != null) { handler.select_server = delegate (Server server, Server selServer) { return server.id == cfg.server.id; }; } else if (!string.IsNullOrEmpty(cfg.id)) { handler.select_server = delegate (Server server, Server selServer) { return server.group == cfg.id; }; } if (cfg.type == PortMapType.Forward) // tunnel { byte[] addr = System.Text.Encoding.UTF8.GetBytes(cfg.server_addr); byte[] newFirstPacket = new byte[_firstPacketLength + addr.Length + 4]; newFirstPacket[0] = 3; newFirstPacket[1] = (byte)addr.Length; Array.Copy(addr, 0, newFirstPacket, 2, addr.Length); newFirstPacket[addr.Length + 2] = (byte)(cfg.server_port / 256); newFirstPacket[addr.Length + 3] = (byte)(cfg.server_port % 256); Array.Copy(_firstPacket, 0, newFirstPacket, addr.Length + 4, _firstPacketLength); _remoteHeaderSendBuffer = newFirstPacket; handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, null); } else if (_connectionUDP == null && cfg.type == PortMapType.RuleProxy && (new Socks5Forwarder(_config, _IPRange)).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol)) { } else { handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, "socks5"); } Dispose(); return; } } else { if (_connectionUDP == null && new Socks5Forwarder(_config, _IPRange).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection, local_sendback_protocol)) { } else { handler.Start(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, local_sendback_protocol); } Dispose(); return; } Dispose(); Close(); } private void Dispose() { _transfer = null; _IPRange = null; _firstPacket = null; _connection = null; _connectionUDP = null; _connetionRecvBuffer = null; _remoteHeaderSendBuffer = null; httpProxyState = null; } } } ================================================ FILE: shadowsocks-csharp/Controller/ProxySocket.cs ================================================ using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using Shadowsocks.Encryption; using Shadowsocks.Model; using Shadowsocks.Obfs; namespace Shadowsocks.Controller { public abstract class IHandler { public delegate void InvokeHandler(); public abstract void Shutdown(); } public class CallbackState { public byte[] buffer; public int size; public int protocol_size; public object state; } public class ProxySocketTun { protected Socket _socket; protected EndPoint _socketEndPoint; protected IPEndPoint _remoteUDPEndPoint; protected bool _proxy; protected string _proxy_server; protected int _proxy_udp_port; protected const int RecvSize = 1460 * 2; private byte[] SendEncryptBuffer = new byte[RecvSize]; private byte[] ReceiveDecryptBuffer = new byte[RecvSize * 2]; protected bool _close; public ProxySocketTun(Socket socket) { _socket = socket; } public ProxySocketTun(AddressFamily af, SocketType type, ProtocolType protocol) { _socket = new Socket(af, type, protocol); } public Socket GetSocket() { return _socket; } public bool IsClose { get { return _close; } } public bool GoS5Proxy { get { return _proxy; } set { _proxy = value; } } public AddressFamily AddressFamily { get { return _socket.AddressFamily; } } public int Available { get { return _socket.Available; } } public void Shutdown(SocketShutdown how) { _socket.Shutdown(how); } public void Close() { _socket.Close(); _socket = null; SendEncryptBuffer = null; ReceiveDecryptBuffer = null; } public IAsyncResult BeginConnect(EndPoint ep, AsyncCallback callback, object state) { _close = false; _socketEndPoint = ep; return _socket.BeginConnect(ep, callback, state); } public void EndConnect(IAsyncResult ar) { _socket.EndConnect(ar); } public int Receive(byte[] buffer, int size, SocketFlags flags) { return _socket.Receive(buffer, size, SocketFlags.None); } public IAsyncResult BeginReceive(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.buffer = buffer; st.size = size; st.state = state; return _socket.BeginReceive(buffer, 0, size, flags, callback, st); } public int EndReceive(IAsyncResult ar) { int bytesRead = _socket.EndReceive(ar); if (bytesRead > 0) { CallbackState st = (CallbackState)ar.AsyncState; st.size = bytesRead; return bytesRead; } else { _close = true; } return bytesRead; } public int SendAll(byte[] buffer, int size, SocketFlags flags) { int sendSize = _socket.Send(buffer, size, 0); while (sendSize < size) { int new_size = _socket.Send(buffer, sendSize, size - sendSize, 0); sendSize += new_size; } return size; } public virtual int Send(byte[] buffer, int size, SocketFlags flags) { return SendAll(buffer, size, 0); } public int BeginSend(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.size = size; st.state = state; _socket.BeginSend(buffer, 0, size, 0, callback, st); return size; } public int EndSend(IAsyncResult ar) { return _socket.EndSend(ar); } public IAsyncResult BeginReceiveFrom(byte[] buffer, int size, SocketFlags flags, ref EndPoint ep, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.buffer = buffer; st.size = size; st.state = state; return _socket.BeginReceiveFrom(buffer, 0, size, flags, ref ep, callback, st); } public int GetAsyncResultSize(IAsyncResult ar) { CallbackState st = (CallbackState)ar.AsyncState; return st.size; } public byte[] GetAsyncResultBuffer(IAsyncResult ar) { CallbackState st = (CallbackState)ar.AsyncState; return st.buffer; } public bool ConnectSocks5ProxyServer(string strRemoteHost, int iRemotePort, bool udp, string socks5RemoteUsername, string socks5RemotePassword) { int socketErrorCode = (int)SocketError.ConnectionReset; _proxy = true; //构造Socks5代理服务器第一连接头(无用户名密码) byte[] bySock5Send = new Byte[10]; bySock5Send[0] = 5; bySock5Send[1] = (socks5RemoteUsername.Length == 0 ? (byte)1 : (byte)2); bySock5Send[2] = 0; bySock5Send[3] = 2; //发送Socks5代理第一次连接信息 _socket.Send(bySock5Send, bySock5Send[1] + 2, SocketFlags.None); byte[] bySock5Receive = new byte[32]; int iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (iRecCount < 2) { throw new SocketException(socketErrorCode); } if (bySock5Receive[0] != 5 || (bySock5Receive[1] != 0 && bySock5Receive[1] != 2)) { throw new SocketException(socketErrorCode); } if (bySock5Receive[1] != 0) // auth { if (bySock5Receive[1] == 2) { if (socks5RemoteUsername.Length == 0) { throw new SocketException(socketErrorCode); } else { bySock5Send = new Byte[socks5RemoteUsername.Length + socks5RemotePassword.Length + 3]; bySock5Send[0] = 1; bySock5Send[1] = (Byte)socks5RemoteUsername.Length; for (int i = 0; i < socks5RemoteUsername.Length; ++i) { bySock5Send[2 + i] = (Byte)socks5RemoteUsername[i]; } bySock5Send[socks5RemoteUsername.Length + 2] = (Byte)socks5RemotePassword.Length; for (int i = 0; i < socks5RemotePassword.Length; ++i) { bySock5Send[socks5RemoteUsername.Length + 3 + i] = (Byte)socks5RemotePassword[i]; } _socket.Send(bySock5Send, bySock5Send.Length, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (bySock5Receive[0] != 1 || bySock5Receive[1] != 0) { throw new SocketException((int)SocketError.ConnectionRefused); } } } else { return false; } } // connect if (!udp) // TCP { List dataSock5Send = new List(); dataSock5Send.Add(5); dataSock5Send.Add(1); dataSock5Send.Add(0); IPAddress ipAdd; bool parsed = IPAddress.TryParse(strRemoteHost, out ipAdd); if (ipAdd == null) { dataSock5Send.Add(3); // remote DNS resolve dataSock5Send.Add((byte)strRemoteHost.Length); for (int i = 0; i < strRemoteHost.Length; ++i) { dataSock5Send.Add((byte)strRemoteHost[i]); } } else { byte[] addBytes = ipAdd.GetAddressBytes(); if (addBytes.GetLength(0) > 4) { dataSock5Send.Add(4); // IPv6 for (int i = 0; i < 16; ++i) { dataSock5Send.Add(addBytes[i]); } } else { dataSock5Send.Add(1); // IPv4 for (int i = 0; i < 4; ++i) { dataSock5Send.Add(addBytes[i]); } } } dataSock5Send.Add((byte)(iRemotePort / 256)); dataSock5Send.Add((byte)(iRemotePort % 256)); _socket.Send(dataSock5Send.ToArray(), dataSock5Send.Count, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (iRecCount < 2 || bySock5Receive[0] != 5 || bySock5Receive[1] != 0) { throw new SocketException(socketErrorCode); //throw new Exception("第二次连接Socks5代理返回数据出错。"); } return true; } else // UDP { List dataSock5Send = new List(); dataSock5Send.Add(5); dataSock5Send.Add(3); dataSock5Send.Add(0); IPAddress ipAdd = ((IPEndPoint)_socketEndPoint).Address; { byte[] addBytes = ipAdd.GetAddressBytes(); if (addBytes.GetLength(0) > 4) { dataSock5Send.Add(4); // IPv6 for (int i = 0; i < 16; ++i) { dataSock5Send.Add(addBytes[i]); } } else { dataSock5Send.Add(1); // IPv4 for (int i = 0; i < 4; ++i) { dataSock5Send.Add(addBytes[i]); } } } dataSock5Send.Add((byte)(0)); dataSock5Send.Add((byte)(0)); _socket.Send(dataSock5Send.ToArray(), dataSock5Send.Count, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (bySock5Receive[0] != 5 || bySock5Receive[1] != 0) { throw new SocketException(socketErrorCode); //throw new Exception("第二次连接Socks5代理返回数据出错。"); } else { bool ipv6 = bySock5Receive[0] == 4; byte[] addr; int port; if (!ipv6) { addr = new byte[4]; Array.Copy(bySock5Receive, 4, addr, 0, 4); port = bySock5Receive[8] * 0x100 + bySock5Receive[9]; } else { addr = new byte[16]; Array.Copy(bySock5Receive, 4, addr, 0, 16); port = bySock5Receive[20] * 0x100 + bySock5Receive[21]; } ipAdd = new IPAddress(addr); _remoteUDPEndPoint = new IPEndPoint(ipAdd, port); } return true; } } public void SetTcpServer(string server, int port) { _proxy_server = server; _proxy_udp_port = port; } public void SetUdpServer(string server, int port) { _proxy_server = server; _proxy_udp_port = port; } public void SetUdpEndPoint(IPEndPoint ep) { _remoteUDPEndPoint = ep; } public bool ConnectHttpProxyServer(string strRemoteHost, int iRemotePort, string socks5RemoteUsername, string socks5RemotePassword, string proxyUserAgent) { _proxy = true; IPAddress ipAdd; bool parsed = IPAddress.TryParse(strRemoteHost, out ipAdd); if (ipAdd != null) { strRemoteHost = ipAdd.ToString(); } string host = (strRemoteHost.IndexOf(':') >= 0 ? "[" + strRemoteHost + "]" : strRemoteHost) + ":" + iRemotePort.ToString(); string authstr = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(socks5RemoteUsername + ":" + socks5RemotePassword)); string cmd = "CONNECT " + host + " HTTP/1.0\r\n" + "Host: " + host + "\r\n"; if (!string.IsNullOrEmpty(proxyUserAgent)) cmd += "User-Agent: " + proxyUserAgent + "\r\n"; cmd += "Proxy-Connection: Keep-Alive\r\n"; if (socks5RemoteUsername.Length > 0) cmd += "Proxy-Authorization: Basic " + authstr + "\r\n"; cmd += "\r\n"; byte[] httpData = System.Text.Encoding.UTF8.GetBytes(cmd); _socket.Send(httpData, httpData.Length, SocketFlags.None); byte[] byReceive = new byte[1024]; int iRecCount = _socket.Receive(byReceive, byReceive.Length, SocketFlags.None); if (iRecCount > 13) { string data = System.Text.Encoding.UTF8.GetString(byReceive, 0, iRecCount); string[] data_part = data.Split(' '); if (data_part.Length > 1 && data_part[1] == "200") { return true; } } return false; } } public class ProxySocketTunLocal : ProxySocketTun { public string local_sendback_protocol; public ProxySocketTunLocal(Socket socket) : base(socket) { } public ProxySocketTunLocal(AddressFamily af, SocketType type, ProtocolType protocol) : base(af, type, protocol) { } public override int Send(byte[] buffer, int size, SocketFlags flags) { if (local_sendback_protocol != null) { if (local_sendback_protocol == "http") { byte[] data = System.Text.Encoding.UTF8.GetBytes("HTTP/1.1 200 Connection Established\r\n\r\n"); _socket.Send(data, data.Length, 0); } else if (local_sendback_protocol == "socks5") { if (_socket.AddressFamily == AddressFamily.InterNetwork) { byte[] response = { 5, 0, 0, 1, 0, 0, 0, 0, 0, 0 }; _socket.Send(response); } else { byte[] response = { 5, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; _socket.Send(response); } } local_sendback_protocol = null; } return SendAll(buffer, size, 0); } } class ProxyEncryptSocket { protected Socket _socket; protected EndPoint _socketEndPoint; protected IPEndPoint _remoteUDPEndPoint; protected IEncryptor _encryptor; protected object _encryptionLock = new object(); protected object _decryptionLock = new object(); protected string _method; protected string _password; public IObfs _protocol; public IObfs _obfs; //protected object obfsLock = new object(); protected bool _proxy; protected string _proxy_server; protected int _proxy_udp_port; //private bool header_sent = false; public const int MTU = 1492; public const int MSS = MTU - 40; protected const int RecvSize = MSS * 4; public int TcpMSS = MSS; public int RecvBufferSize; public int OverHead; private byte[] SendEncryptBuffer = new byte[RecvSize]; private byte[] ReceiveDecryptBuffer = new byte[RecvSize * 2]; protected bool _close; public ProxyEncryptSocket(AddressFamily af, SocketType type, ProtocolType protocol) { _socket = new Socket(af, type, protocol); } public Socket GetSocket() { return _socket; } public bool IsClose { get { return _close; } } public bool GoS5Proxy { get { return _proxy; } set { _proxy = value; } } public bool CanSendKeepAlive { get { return _protocol.isKeepAlive(); } } public bool isProtocolSendback { get { return _protocol.isAlwaysSendback(); } } public bool isObfsSendback { get { return _obfs.isAlwaysSendback(); } } public AddressFamily AddressFamily { get { return _socket.AddressFamily; } } public int Available { get { return _socket.Available; } } public void Shutdown(SocketShutdown how) { _socket.Shutdown(how); } public void Close() { _socket.Close(); if (_protocol != null) { _protocol.Dispose(); _protocol = null; } if (_obfs != null) { _obfs.Dispose(); _obfs = null; } lock (_encryptionLock) { lock (_decryptionLock) { if (_encryptor != null) { ((IDisposable)_encryptor).Dispose(); _encryptor = null; } } } _socket = null; SendEncryptBuffer = null; ReceiveDecryptBuffer = null; } public IAsyncResult BeginConnect(EndPoint ep, AsyncCallback callback, object state) { _close = false; _socketEndPoint = ep; return _socket.BeginConnect(ep, callback, state); } public void EndConnect(IAsyncResult ar) { _socket.EndConnect(ar); } public void CreateEncryptor(string method, string password) { _encryptor = EncryptorFactory.GetEncryptor(method, password, true); _method = method; _password = password; } public void SetProtocol(IObfs protocol) { _protocol = protocol; } public void SetObfs(IObfs obfs) { _obfs = obfs; } public void SetObfsPlugin(Server server, int head_len) { lock (server) // need a server lock { if (server.getProtocolData() == null) { server.setProtocolData(_protocol.InitData()); } if (server.getObfsData() == null) { server.setObfsData(_obfs.InitData()); } } int mss = MSS; string server_addr = server.server; OverHead = _protocol.GetOverhead() + _obfs.GetOverhead(); RecvBufferSize = RecvSize - OverHead; if (_proxy_server != null) server_addr = _proxy_server; _protocol.SetServerInfo(new ServerInfo(server_addr, server.server_port, server.protocolparam??"", server.getProtocolData(), _encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss, OverHead, RecvBufferSize)); _obfs.SetServerInfo(new ServerInfo(server_addr, server.server_port, server.obfsparam??"", server.getObfsData(), _encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss, OverHead, RecvBufferSize)); } public int Receive(byte[] recv_buffer, int size, SocketFlags flags, out int bytesRead, out int protocolSize, out bool sendback) { bytesRead = _socket.Receive(recv_buffer, size, flags); protocolSize = 0; if (bytesRead > 0) { lock (_decryptionLock) { int bytesToSend = 0; int obfsRecvSize; byte[] remoteRecvObfsBuffer = _obfs.ClientDecode(recv_buffer, bytesRead, out obfsRecvSize, out sendback); if (obfsRecvSize > 0) { Util.Utils.SetArrayMinSize(ref ReceiveDecryptBuffer, obfsRecvSize); _encryptor.Decrypt(remoteRecvObfsBuffer, obfsRecvSize, ReceiveDecryptBuffer, out bytesToSend); int outlength; protocolSize = bytesToSend; byte[] buffer = _protocol.ClientPostDecrypt(ReceiveDecryptBuffer, bytesToSend, out outlength); TcpMSS = _protocol.GetTcpMSS(); //if (recv_buffer.Length < outlength) //ASSERT Array.Copy(buffer, 0, recv_buffer, 0, outlength); return outlength; } } return 0; } else { sendback = false; _close = true; } return bytesRead; } public IAsyncResult BeginReceive(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.buffer = buffer; st.size = size; st.state = state; return _socket.BeginReceive(buffer, 0, size, flags, callback, st); } public int EndReceive(IAsyncResult ar, out bool sendback) { int bytesRead = _socket.EndReceive(ar); sendback = false; if (bytesRead > 0) { CallbackState st = (CallbackState)ar.AsyncState; st.size = bytesRead; lock (_decryptionLock) { int bytesToSend = 0; int obfsRecvSize; byte[] remoteRecvObfsBuffer = _obfs.ClientDecode(st.buffer, bytesRead, out obfsRecvSize, out sendback); if (obfsRecvSize > 0) { Util.Utils.SetArrayMinSize(ref ReceiveDecryptBuffer, obfsRecvSize); _encryptor.Decrypt(remoteRecvObfsBuffer, obfsRecvSize, ReceiveDecryptBuffer, out bytesToSend); int outlength; st.protocol_size = bytesToSend; byte[] buffer = _protocol.ClientPostDecrypt(ReceiveDecryptBuffer, bytesToSend, out outlength); TcpMSS = _protocol.GetTcpMSS(); if (st.buffer.Length < outlength) { Array.Resize(ref st.buffer, outlength); } Array.Copy(buffer, 0, st.buffer, 0, outlength); return outlength; } } return 0; } else { _close = true; } return bytesRead; } public int SendAll(byte[] buffer, int size, SocketFlags flags) { int sendSize = _socket.Send(buffer, size, 0); while (sendSize < size) { int new_size = _socket.Send(buffer, sendSize, size - sendSize, 0); sendSize += new_size; } return size; } public int Send(byte[] buffer, int size, SocketFlags flags) { int bytesToSend = 0; int obfsSendSize; byte[] obfsBuffer; lock (_encryptionLock) { int outlength; //if (!header_sent) //{ // header_sent = true; // if (buffer[0] == 3 && _method == "none") // { // for (int i = 0; i < buffer[1]; ++i) // { // buffer[i + 2] |= 0x80; // } // buffer[0] = 2; // } //} byte[] bytesToEncrypt = _protocol.ClientPreEncrypt(buffer, size, out outlength); if (bytesToEncrypt == null) return 0; Util.Utils.SetArrayMinSize(ref SendEncryptBuffer, outlength + 32); _encryptor.Encrypt(bytesToEncrypt, outlength, SendEncryptBuffer, out bytesToSend); obfsBuffer = _obfs.ClientEncode(SendEncryptBuffer, bytesToSend, out obfsSendSize); } return SendAll(obfsBuffer, obfsSendSize, 0); } public IAsyncResult BeginReceiveFrom(byte[] buffer, int size, SocketFlags flags, ref EndPoint ep, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.buffer = buffer; st.size = size; st.state = state; return _socket.BeginReceiveFrom(buffer, 0, size, flags, ref ep, callback, st); } private bool RemoveRemoteUDPRecvBufferHeader(byte[] remoteRecvBuffer, ref int bytesRead) { if (_proxy) { if (bytesRead < 7) { return false; } int port = -1; if (remoteRecvBuffer[3] == 1) { int head = 3 + 1 + 4 + 2; bytesRead = bytesRead - head; port = remoteRecvBuffer[head - 2] * 0x100 + remoteRecvBuffer[head - 1]; Array.Copy(remoteRecvBuffer, head, remoteRecvBuffer, 0, bytesRead); } else if (remoteRecvBuffer[3] == 4) { int head = 3 + 1 + 16 + 2; bytesRead = bytesRead - head; port = remoteRecvBuffer[head - 2] * 0x100 + remoteRecvBuffer[head - 1]; Array.Copy(remoteRecvBuffer, head, remoteRecvBuffer, 0, bytesRead); } else if (remoteRecvBuffer[3] == 3) { int head = 3 + 1 + 1 + remoteRecvBuffer[4] + 2; bytesRead = bytesRead - head; port = remoteRecvBuffer[head - 2] * 0x100 + remoteRecvBuffer[head - 1]; Array.Copy(remoteRecvBuffer, head, remoteRecvBuffer, 0, bytesRead); } else { return false; } //if (port != server.server_port && port != server.server_udp_port) //{ // return false; //} } return true; } protected static byte[] ParseUDPHeader(byte[] buffer, ref int len) { if (buffer.Length == 0) return buffer; if (buffer[0] == 0x81) { len = len - 1; byte[] ret = new byte[len]; Array.Copy(buffer, 1, ret, 0, len); return ret; } if (buffer[0] == 0x80 && len >= 2) { int ofbs_len = buffer[1]; if (ofbs_len + 2 < len) { len = len - ofbs_len - 2; byte[] ret = new byte[len]; Array.Copy(buffer, ofbs_len + 2, ret, 0, len); return ret; } } if (buffer[0] == 0x82 && len >= 3) { int ofbs_len = (buffer[1] << 8) + buffer[2]; if (ofbs_len + 3 < len) { len = len - ofbs_len - 3; byte[] ret = new byte[len]; Array.Copy(buffer, ofbs_len + 3, ret, 0, len); return ret; } } if (len < buffer.Length) { byte[] ret = new byte[len]; Array.Copy(buffer, ret, len); return ret; } return buffer; } protected void AddRemoteUDPRecvBufferHeader(byte[] decryptBuffer, byte[] remoteSendBuffer, ref int bytesToSend) { Array.Copy(decryptBuffer, 0, remoteSendBuffer, 3, bytesToSend); remoteSendBuffer[0] = 0; remoteSendBuffer[1] = 0; remoteSendBuffer[2] = 0; bytesToSend += 3; } public int EndReceiveFrom(IAsyncResult ar, ref EndPoint ep) { int bytesRead = _socket.EndReceiveFrom(ar, ref ep); if (bytesRead > 0) { CallbackState st = (CallbackState)ar.AsyncState; st.size = bytesRead; int bytesToSend; if (!RemoveRemoteUDPRecvBufferHeader(st.buffer, ref bytesRead)) { return 0; // drop } byte[] remoteSendBuffer = new byte[65536]; byte[] obfsBuffer; lock (_decryptionLock) { byte[] decryptBuffer = new byte[65536]; _encryptor.ResetDecrypt(); _encryptor.Decrypt(st.buffer, bytesRead, decryptBuffer, out bytesToSend); obfsBuffer = _protocol.ClientUdpPostDecrypt(decryptBuffer, bytesToSend, out bytesToSend); decryptBuffer = ParseUDPHeader(obfsBuffer, ref bytesToSend); AddRemoteUDPRecvBufferHeader(decryptBuffer, remoteSendBuffer, ref bytesToSend); } Array.Copy(remoteSendBuffer, 0, st.buffer, 0, bytesToSend); return bytesToSend; } else { _close = true; } return bytesRead; } public int BeginSendTo(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state) { CallbackState st = new CallbackState(); st.buffer = buffer; st.size = size; st.state = state; int bytesToSend; byte[] bytesToEncrypt = null; byte[] connetionSendBuffer = new byte[65536]; int bytes_beg = 3; int length = size - bytes_beg; bytesToEncrypt = new byte[length]; Array.Copy(buffer, bytes_beg, bytesToEncrypt, 0, length); lock (_encryptionLock) { _encryptor.ResetEncrypt(); _protocol.SetServerInfoIV(_encryptor.getIV()); int obfsSendSize; byte[] obfsBuffer = _protocol.ClientUdpPreEncrypt(bytesToEncrypt, length, out obfsSendSize); _encryptor.Encrypt(obfsBuffer, obfsSendSize, connetionSendBuffer, out bytesToSend); } if (_proxy) { IPAddress ipAddress; string serverURI = _proxy_server; int serverPort = _proxy_udp_port; bool parsed = IPAddress.TryParse(serverURI, out ipAddress); if (!parsed) { bytesToEncrypt = new byte[bytes_beg + 1 + 1 + serverURI.Length + 2 + bytesToSend]; Array.Copy(connetionSendBuffer, 0, bytesToEncrypt, bytes_beg + 1 + 1 + serverURI.Length + 2, bytesToSend); bytesToEncrypt[0] = 0; bytesToEncrypt[1] = 0; bytesToEncrypt[2] = 0; bytesToEncrypt[3] = (byte)3; bytesToEncrypt[4] = (byte)serverURI.Length; for (int i = 0; i < serverURI.Length; ++i) { bytesToEncrypt[5 + i] = (byte)serverURI[i]; } bytesToEncrypt[5 + serverURI.Length] = (byte)(serverPort / 0x100); bytesToEncrypt[5 + serverURI.Length + 1] = (byte)(serverPort % 0x100); } else { byte[] addBytes = ipAddress.GetAddressBytes(); bytesToEncrypt = new byte[bytes_beg + 1 + addBytes.Length + 2 + bytesToSend]; Array.Copy(connetionSendBuffer, 0, bytesToEncrypt, bytes_beg + 1 + addBytes.Length + 2, bytesToSend); bytesToEncrypt[0] = 0; bytesToEncrypt[1] = 0; bytesToEncrypt[2] = 0; bytesToEncrypt[3] = ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? (byte)4 : (byte)1; for (int i = 0; i < addBytes.Length; ++i) { bytesToEncrypt[4 + i] = addBytes[i]; } bytesToEncrypt[4 + addBytes.Length] = (byte)(serverPort / 0x100); bytesToEncrypt[4 + addBytes.Length + 1] = (byte)(serverPort % 0x100); } bytesToSend = bytesToEncrypt.Length; Array.Copy(bytesToEncrypt, connetionSendBuffer, bytesToSend); } _socket.BeginSendTo(connetionSendBuffer, 0, bytesToSend, flags, _remoteUDPEndPoint, callback, st); return bytesToSend; } public int EndSendTo(IAsyncResult ar) { return _socket.EndSendTo(ar); } public int GetAsyncResultSize(IAsyncResult ar) { CallbackState st = (CallbackState)ar.AsyncState; return st.size; } public int GetAsyncProtocolSize(IAsyncResult ar) { CallbackState st = (CallbackState)ar.AsyncState; return st.protocol_size; } public byte[] GetAsyncResultBuffer(IAsyncResult ar) { CallbackState st = (CallbackState)ar.AsyncState; return st.buffer; } public IPEndPoint GetProxyUdpEndPoint() { return _remoteUDPEndPoint; } public bool ConnectSocks5ProxyServer(string strRemoteHost, int iRemotePort, bool udp, string socks5RemoteUsername, string socks5RemotePassword) { int socketErrorCode = (int)SocketError.ConnectionReset; _proxy = true; //构造Socks5代理服务器第一连接头(无用户名密码) byte[] bySock5Send = new Byte[10]; bySock5Send[0] = 5; bySock5Send[1] = (socks5RemoteUsername.Length == 0 ? (byte)1 : (byte)2); bySock5Send[2] = 0; bySock5Send[3] = 2; //发送Socks5代理第一次连接信息 SendAll(bySock5Send, 2 + bySock5Send[1], SocketFlags.None); byte[] bySock5Receive = new byte[32]; int iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (iRecCount < 2) { throw new SocketException(socketErrorCode); //throw new Exception("不能获得代理服务器正确响应。"); } if (bySock5Receive[0] != 5 || (bySock5Receive[1] != 0 && bySock5Receive[1] != 2)) { throw new SocketException(socketErrorCode); //throw new Exception("代理服务其返回的响应错误。"); } if (bySock5Receive[1] != 0) // auth { if (bySock5Receive[1] == 2) { if (socks5RemoteUsername.Length == 0) { throw new SocketException(socketErrorCode); //throw new Exception("代理服务器需要进行身份确认。"); } else { bySock5Send = new Byte[socks5RemoteUsername.Length + socks5RemotePassword.Length + 3]; bySock5Send[0] = 1; bySock5Send[1] = (Byte)socks5RemoteUsername.Length; for (int i = 0; i < socks5RemoteUsername.Length; ++i) { bySock5Send[2 + i] = (Byte)socks5RemoteUsername[i]; } bySock5Send[socks5RemoteUsername.Length + 2] = (Byte)socks5RemotePassword.Length; for (int i = 0; i < socks5RemotePassword.Length; ++i) { bySock5Send[socks5RemoteUsername.Length + 3 + i] = (Byte)socks5RemotePassword[i]; } SendAll(bySock5Send, bySock5Send.Length, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (bySock5Receive[0] != 1 || bySock5Receive[1] != 0) { throw new SocketException((int)SocketError.ConnectionRefused); } } } else { return false; } } // connect if (!udp) // TCP { List dataSock5Send = new List(); dataSock5Send.Add(5); dataSock5Send.Add(1); dataSock5Send.Add(0); IPAddress ipAdd; bool parsed = IPAddress.TryParse(strRemoteHost, out ipAdd); if (ipAdd == null) { dataSock5Send.Add(3); // remote DNS resolve dataSock5Send.Add((byte)strRemoteHost.Length); for (int i = 0; i < strRemoteHost.Length; ++i) { dataSock5Send.Add((byte)strRemoteHost[i]); } } else { byte[] addBytes = ipAdd.GetAddressBytes(); if (addBytes.GetLength(0) > 4) { dataSock5Send.Add(4); // IPv6 for (int i = 0; i < 16; ++i) { dataSock5Send.Add(addBytes[i]); } } else { dataSock5Send.Add(1); // IPv4 for (int i = 0; i < 4; ++i) { dataSock5Send.Add(addBytes[i]); } } } dataSock5Send.Add((byte)(iRemotePort / 256)); dataSock5Send.Add((byte)(iRemotePort % 256)); SendAll(dataSock5Send.ToArray(), dataSock5Send.Count, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (iRecCount < 2 || bySock5Receive[0] != 5 || bySock5Receive[1] != 0) { throw new SocketException(socketErrorCode); //throw new Exception("第二次连接Socks5代理返回数据出错。"); } return true; } else // UDP { List dataSock5Send = new List(); dataSock5Send.Add(5); dataSock5Send.Add(3); dataSock5Send.Add(0); IPAddress ipAdd = ((IPEndPoint)_socketEndPoint).Address; { byte[] addBytes = ipAdd.GetAddressBytes(); if (addBytes.GetLength(0) > 4) { dataSock5Send.Add(4); // IPv6 for (int i = 0; i < 16; ++i) { dataSock5Send.Add(addBytes[i]); } } else { dataSock5Send.Add(1); // IPv4 for (int i = 0; i < 4; ++i) { dataSock5Send.Add(addBytes[i]); } } } dataSock5Send.Add((byte)(0)); dataSock5Send.Add((byte)(0)); SendAll(dataSock5Send.ToArray(), dataSock5Send.Count, SocketFlags.None); iRecCount = _socket.Receive(bySock5Receive, bySock5Receive.Length, SocketFlags.None); if (bySock5Receive[0] != 5 || bySock5Receive[1] != 0) { throw new SocketException(socketErrorCode); //throw new Exception("第二次连接Socks5代理返回数据出错。"); } else { bool ipv6 = bySock5Receive[0] == 4; byte[] addr; int port; if (!ipv6) { addr = new byte[4]; Array.Copy(bySock5Receive, 4, addr, 0, 4); port = bySock5Receive[8] * 0x100 + bySock5Receive[9]; } else { addr = new byte[16]; Array.Copy(bySock5Receive, 4, addr, 0, 16); port = bySock5Receive[20] * 0x100 + bySock5Receive[21]; } ipAdd = new IPAddress(addr); _remoteUDPEndPoint = new IPEndPoint(ipAdd, port); } return true; } } public void SetTcpServer(string server, int port) { _proxy_server = server; _proxy_udp_port = port; } public void SetUdpServer(string server, int port) { _proxy_server = server; _proxy_udp_port = port; } public void SetUdpEndPoint(IPEndPoint ep) { _remoteUDPEndPoint = ep; } public bool ConnectHttpProxyServer(string strRemoteHost, int iRemotePort, string socks5RemoteUsername, string socks5RemotePassword, string proxyUserAgent) { _proxy = true; IPAddress ipAdd; bool parsed = IPAddress.TryParse(strRemoteHost, out ipAdd); if (ipAdd != null) { strRemoteHost = ipAdd.ToString(); } string host = (strRemoteHost.IndexOf(':') >= 0 ? "[" + strRemoteHost + "]" : strRemoteHost) + ":" + iRemotePort.ToString(); string authstr = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(socks5RemoteUsername + ":" + socks5RemotePassword)); string cmd = "CONNECT " + host + " HTTP/1.0\r\n" + "Host: " + host + "\r\n"; if (!string.IsNullOrEmpty(proxyUserAgent)) cmd += "User-Agent: " + proxyUserAgent + "\r\n"; cmd += "Proxy-Connection: Keep-Alive\r\n"; if (socks5RemoteUsername.Length > 0) cmd += "Proxy-Authorization: Basic " + authstr + "\r\n"; cmd += "\r\n"; byte[] httpData = System.Text.Encoding.UTF8.GetBytes(cmd); SendAll(httpData, httpData.Length, SocketFlags.None); byte[] byReceive = new byte[1024]; int iRecCount = _socket.Receive(byReceive, byReceive.Length, SocketFlags.None); if (iRecCount > 13) { string data = System.Text.Encoding.UTF8.GetString(byReceive, 0, iRecCount); string[] data_part = data.Split(' '); if (data_part.Length > 1 && data_part[1] == "200") { return true; } } return false; } } } ================================================ FILE: shadowsocks-csharp/Controller/ShadowsocksController.cs ================================================ using System.IO; using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Net.Sockets; using System.Net; namespace Shadowsocks.Controller { public enum ProxyMode { NoModify, Direct, Pac, Global, } public class ShadowsocksController { // controller: // handle user actions // manipulates UI // interacts with low level logic private Listener _listener; private List _port_map_listener; private PACServer _pacServer; private Configuration _config; private ServerTransferTotal _transfer; public IPRangeSet _rangeSet; #if !_CONSOLE private HttpProxyRunner polipoRunner; #endif private GFWListUpdater gfwListUpdater; private bool stopped = false; private bool firstRun = true; public class PathEventArgs : EventArgs { public string Path; } public event EventHandler ConfigChanged; public event EventHandler ToggleModeChanged; public event EventHandler ToggleRuleModeChanged; //public event EventHandler ShareOverLANStatusChanged; public event EventHandler ShowConfigFormEvent; // when user clicked Edit PAC, and PAC file has already created public event EventHandler PACFileReadyToOpen; public event EventHandler UserRuleFileReadyToOpen; public event EventHandler UpdatePACFromGFWListCompleted; public event ErrorEventHandler UpdatePACFromGFWListError; public event ErrorEventHandler Errored; public ShadowsocksController() { _config = Configuration.Load(); _transfer = ServerTransferTotal.Load(); foreach (Server server in _config.configs) { if (_transfer.servers.ContainsKey(server.server)) { ServerSpeedLog log = new ServerSpeedLog(((ServerTrans)_transfer.servers[server.server]).totalUploadBytes, ((ServerTrans)_transfer.servers[server.server]).totalDownloadBytes); server.SetServerSpeedLog(log); } } } public void Start() { Reload(); } protected void ReportError(Exception e) { Errored?.Invoke(this, new ErrorEventArgs(e)); } public void ReloadIPRange() { _rangeSet = new IPRangeSet(); _rangeSet.LoadChn(); if (_config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina) { _rangeSet.Reverse(); } } // always return copy public Configuration GetConfiguration() { return Configuration.Load(); } public Configuration GetCurrentConfiguration() { return _config; } private int FindFirstMatchServer(Server server, List servers) { for (int i = 0; i < servers.Count; ++i) { if (server.isMatchServer(servers[i])) { return i; } } return -1; } public void AppendConfiguration(Configuration mergeConfig, List servers) { if (servers != null) { for (int j = 0; j < servers.Count; ++j) { if (FindFirstMatchServer(servers[j], mergeConfig.configs) == -1) { mergeConfig.configs.Add(servers[j]); } } } } public List MergeConfiguration(Configuration mergeConfig, List servers) { List missingServers = new List(); if (servers != null) { for (int j = 0; j < servers.Count; ++j) { int i = FindFirstMatchServer(servers[j], mergeConfig.configs); if (i != -1) { bool enable = servers[j].enable; servers[j].CopyServer(mergeConfig.configs[i]); servers[j].enable = enable; } } } for (int i = 0; i < mergeConfig.configs.Count; ++i) { int j = FindFirstMatchServer(mergeConfig.configs[i], servers); if (j == -1) { missingServers.Add(mergeConfig.configs[i]); } } return missingServers; } public Configuration MergeGetConfiguration(Configuration mergeConfig) { Configuration ret = Configuration.Load(); if (mergeConfig != null) { MergeConfiguration(mergeConfig, ret.configs); } return ret; } public void MergeConfiguration(Configuration mergeConfig) { AppendConfiguration(_config, mergeConfig.configs); SaveConfig(_config); } public bool SaveServersConfig(string config) { Configuration new_cfg = Configuration.Load(config); if (new_cfg != null) { SaveServersConfig(new_cfg); return true; } return false; } public void SaveServersConfig(Configuration config) { List missingServers = MergeConfiguration(_config, config.configs); _config.CopyFrom(config); foreach (Server s in missingServers) { s.GetConnections().CloseAll(); } SelectServerIndex(_config.index); } public void SaveServersPortMap(Configuration config) { _config.portMap = config.portMap; SelectServerIndex(_config.index); _config.FlushPortMapCache(); } public bool AddServerBySSURL(string ssURL, string force_group = null, bool toLast = false) { if (ssURL.StartsWith("ss://", StringComparison.OrdinalIgnoreCase) || ssURL.StartsWith("ssr://", StringComparison.OrdinalIgnoreCase)) { try { var server = new Server(ssURL, force_group); if (toLast) { _config.configs.Add(server); } else { int index = _config.index + 1; if (index < 0 || index > _config.configs.Count) index = _config.configs.Count; _config.configs.Insert(index, server); } SaveConfig(_config); return true; } catch (Exception e) { Logging.LogUsefulException(e); return false; } } else { return false; } } public void ToggleMode(ProxyMode mode) { _config.sysProxyMode = (int)mode; SaveConfig(_config); if (ToggleModeChanged != null) { ToggleModeChanged(this, new EventArgs()); } } public void ToggleRuleMode(int mode) { _config.proxyRuleMode = mode; SaveConfig(_config); if (ToggleRuleModeChanged != null) { ToggleRuleModeChanged(this, new EventArgs()); } } public void ToggleSelectRandom(bool enabled) { _config.random = enabled; SaveConfig(_config); } public void ToggleSameHostForSameTargetRandom(bool enabled) { _config.sameHostForSameTarget = enabled; SaveConfig(_config); } public void SelectServerIndex(int index) { _config.index = index; SaveConfig(_config); } public void Stop() { if (stopped) { return; } stopped = true; if (_port_map_listener != null) { foreach (Listener l in _port_map_listener) { l.Stop(); } _port_map_listener = null; } if (_listener != null) { _listener.Stop(); } #if !_CONSOLE if (polipoRunner != null) { polipoRunner.Stop(); } if (_config.sysProxyMode != (int)ProxyMode.NoModify && _config.sysProxyMode != (int)ProxyMode.Direct) { SystemProxy.Update(_config, true); } #endif ServerTransferTotal.Save(_transfer); } public void ClearTransferTotal(string server_addr) { _transfer.Clear(server_addr); foreach (Server server in _config.configs) { if (server.server == server_addr) { if (_transfer.servers.ContainsKey(server.server)) { server.ServerSpeedLog().ClearTrans(); } } } } public void TouchPACFile() { string pacFilename = _pacServer.TouchPACFile(); if (PACFileReadyToOpen != null) { PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename }); } } public void TouchUserRuleFile() { string userRuleFilename = _pacServer.TouchUserRuleFile(); if (UserRuleFileReadyToOpen != null) { UserRuleFileReadyToOpen(this, new PathEventArgs() { Path = userRuleFilename }); } } public void UpdatePACFromGFWList() { if (gfwListUpdater != null) { gfwListUpdater.UpdatePACFromGFWList(_config); } } public void UpdatePACFromOnlinePac(string url) { if (gfwListUpdater != null) { gfwListUpdater.UpdatePACFromGFWList(_config, url); } } protected void Reload() { if (_port_map_listener != null) { foreach (Listener l in _port_map_listener) { l.Stop(); } _port_map_listener = null; } // some logic in configuration updated the config when saving, we need to read it again _config = MergeGetConfiguration(_config); _config.FlushPortMapCache(); ReloadIPRange(); HostMap hostMap = new HostMap(); hostMap.LoadHostFile(); HostMap.Instance().Clear(hostMap); #if !_CONSOLE if (polipoRunner == null) { polipoRunner = new HttpProxyRunner(); } #endif if (_pacServer == null) { _pacServer = new PACServer(); _pacServer.PACFileChanged += pacServer_PACFileChanged; } _pacServer.UpdateConfiguration(_config); if (gfwListUpdater == null) { gfwListUpdater = new GFWListUpdater(); gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted; gfwListUpdater.Error += pacServer_PACUpdateError; } // 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 // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open bool _firstRun = firstRun; for (int i = 1; i <= 5; ++i) { _firstRun = false; try { if (_listener != null && !_listener.isConfigChange(_config)) { Local local = new Local(_config, _transfer, _rangeSet); _listener.GetServices()[0] = local; #if !_CONSOLE if (polipoRunner.HasExited()) { polipoRunner.Stop(); polipoRunner.Start(_config); _listener.GetServices()[3] = new HttpPortForwarder(polipoRunner.RunningPort, _config); } #endif } else { if (_listener != null) { _listener.Stop(); _listener = null; } #if !_CONSOLE polipoRunner.Stop(); polipoRunner.Start(_config); #endif Local local = new Local(_config, _transfer, _rangeSet); List services = new List(); services.Add(local); services.Add(_pacServer); services.Add(new APIServer(this, _config)); #if !_CONSOLE services.Add(new HttpPortForwarder(polipoRunner.RunningPort, _config)); #endif _listener = new Listener(services); _listener.Start(_config, 0); } break; } catch (Exception e) { // translate Microsoft language into human language // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use if (e is SocketException) { SocketException se = (SocketException)e; if (se.SocketErrorCode == SocketError.AccessDenied) { e = new Exception(I18N.GetString("Port already in use") + string.Format(" {0}", _config.localPort), e); } } Logging.LogUsefulException(e); if (!_firstRun) { ReportError(e); break; } else { Thread.Sleep(1000 * i * i); } if (_listener != null) { _listener.Stop(); _listener = null; } } } _port_map_listener = new List(); foreach (KeyValuePair pair in _config.GetPortMapCache()) { try { Local local = new Local(_config, _transfer, _rangeSet); List services = new List(); services.Add(local); Listener listener = new Listener(services); listener.Start(_config, pair.Key); _port_map_listener.Add(listener); } catch (Exception e) { // translate Microsoft language into human language // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use if (e is SocketException) { SocketException se = (SocketException)e; if (se.SocketErrorCode == SocketError.AccessDenied) { e = new Exception(I18N.GetString("Port already in use") + string.Format(" {0}", pair.Key), e); } } Logging.LogUsefulException(e); ReportError(e); } } ConfigChanged?.Invoke(this, new EventArgs()); UpdateSystemProxy(); Util.Utils.ReleaseMemory(); } protected void SaveConfig(Configuration newConfig) { Configuration.Save(newConfig); Reload(); } private void UpdateSystemProxy() { #if !_CONSOLE if (_config.sysProxyMode != (int)ProxyMode.NoModify) { SystemProxy.Update(_config, false); } #endif } private void pacServer_PACFileChanged(object sender, EventArgs e) { UpdateSystemProxy(); } private void pacServer_PACUpdateCompleted(object sender, GFWListUpdater.ResultEventArgs e) { if (UpdatePACFromGFWListCompleted != null) UpdatePACFromGFWListCompleted(sender, e); } private void pacServer_PACUpdateError(object sender, ErrorEventArgs e) { if (UpdatePACFromGFWListError != null) UpdatePACFromGFWListError(sender, e); } public void ShowConfigForm(int index) { if (ShowConfigFormEvent != null) { ShowConfigFormEvent(index, new EventArgs()); } } } } ================================================ FILE: shadowsocks-csharp/Controller/Socks5Forwarder.cs ================================================ using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Timers; using OpenDNS; using Shadowsocks.Model; using Shadowsocks.Util; namespace Shadowsocks.Controller { class Socks5Forwarder : Listener.Service { private Configuration _config; private IPRangeSet _IPRange; const int CONNECT_DIRECT = 1; const int CONNECT_LOCALPROXY = 2; const int CONNECT_REMOTEPROXY = 0; public Socks5Forwarder(Configuration config, IPRangeSet IPRange) { _config = config; _IPRange = IPRange; } public bool Handle(byte[] firstPacket, int length, Socket socket) { return Handle(firstPacket, length, socket, null); } public bool Handle(byte[] firstPacket, int length, Socket socket, string local_sendback_protocol) { int handle = IsHandle(firstPacket, length, socket); if (handle > 0) { if (_config.proxyEnable) { new Handler().Start(_config, _IPRange, firstPacket, length, socket, local_sendback_protocol, handle == 2); } else { new Handler().Start(_config, _IPRange, firstPacket, length, socket, local_sendback_protocol, false); } return true; } return false; } public int IsHandle(byte[] firstPacket, int length, Socket socket) { if (length >= 7 && _config.proxyRuleMode != (int)ProxyRuleMode.Disable) { IPAddress ipAddress = null; if (firstPacket[0] == 1) { byte[] addr = new byte[4]; Array.Copy(firstPacket, 1, addr, 0, addr.Length); ipAddress = new IPAddress(addr); } else if (firstPacket[0] == 3) { int len = firstPacket[1]; byte[] addr = new byte[len]; if (length >= len + 2) { Array.Copy(firstPacket, 2, addr, 0, addr.Length); string host = Encoding.UTF8.GetString(firstPacket, 2, len); if (IPAddress.TryParse(host, out ipAddress)) { //pass } else { if ((_config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndChina || _config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina) && _IPRange != null || _config.proxyRuleMode == (int)ProxyRuleMode.UserCustom) { if (!IPAddress.TryParse(host, out ipAddress)) { if (_config.proxyRuleMode == (int)ProxyRuleMode.UserCustom) { Shadowsocks.Model.HostMap hostMap = HostMap.Instance(); string host_addr; if (hostMap.GetHost(host, out host_addr)) { if (!String.IsNullOrEmpty(host_addr)) { string lower_host_addr = host_addr.ToLower(); if (lower_host_addr.StartsWith("reject") || lower_host_addr.StartsWith("direct") ) { return CONNECT_DIRECT; } else if (lower_host_addr.StartsWith("localproxy")) { return CONNECT_LOCALPROXY; } else if (lower_host_addr.StartsWith("remoteproxy")) { return CONNECT_REMOTEPROXY; } else if (lower_host_addr.IndexOf('.') >= 0 || lower_host_addr.IndexOf(':') >= 0) { if (!IPAddress.TryParse(lower_host_addr, out ipAddress)) { // } } } } } if (ipAddress == null) { ipAddress = Utils.DnsBuffer.Get(host); } } if (ipAddress == null) { if (host.IndexOf('.') >= 0) { ipAddress = Util.Utils.QueryDns(host, _config.dnsServer); } else { ipAddress = Utils.QueryDns(host, null); } if (ipAddress != null) { Utils.DnsBuffer.Set(host, new IPAddress(ipAddress.GetAddressBytes())); if (host.IndexOf('.') >= 0) { if (Util.Utils.isLAN(ipAddress)) // assume that it is polution if return LAN address { return CONNECT_REMOTEPROXY; } } } else { Logging.Log(LogLevel.Debug, "DNS query fail: " + host); } } } } } } else if (firstPacket[0] == 4) { byte[] addr = new byte[16]; Array.Copy(firstPacket, 1, addr, 0, addr.Length); ipAddress = new IPAddress(addr); } if (ipAddress != null) { if (_config.proxyRuleMode == (int)ProxyRuleMode.UserCustom) { Shadowsocks.Model.HostMap hostMap = HostMap.Instance(); string host_addr; if (hostMap.GetIP(ipAddress, out host_addr)) { string lower_host_addr = host_addr.ToLower(); if (lower_host_addr.StartsWith("reject") || lower_host_addr.StartsWith("direct") ) { return CONNECT_DIRECT; } else if (lower_host_addr.StartsWith("localproxy")) { return CONNECT_LOCALPROXY; } else if (lower_host_addr.StartsWith("remoteproxy")) { return CONNECT_REMOTEPROXY; } } } else { if (Util.Utils.isLAN(ipAddress)) { return CONNECT_DIRECT; } if ((_config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndChina || _config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina) && _IPRange != null && ipAddress.AddressFamily == AddressFamily.InterNetwork ) { if (_IPRange.IsInIPRange(ipAddress)) { return CONNECT_LOCALPROXY; } Utils.DnsBuffer.Sweep(); } } } } return CONNECT_REMOTEPROXY; } class Handler : IHandler { private IPRangeSet _IPRange; private Configuration _config; private byte[] _firstPacket; private int _firstPacketLength; private ProxySocketTunLocal _local; private ProxySocketTun _remote; private bool _closed = false; private bool _local_proxy = false; private string _remote_host; private int _remote_port; public const int RecvSize = 1460 * 8; // remote receive buffer private byte[] remoteRecvBuffer = new byte[RecvSize]; // connection receive buffer private byte[] connetionRecvBuffer = new byte[RecvSize]; private int _totalRecvSize = 0; protected int TTL = 600; protected System.Timers.Timer timer; protected object timerLock = new object(); protected DateTime lastTimerSetTime; public void Start(Configuration config, IPRangeSet IPRange, byte[] firstPacket, int length, Socket socket, string local_sendback_protocol, bool proxy) { _IPRange = IPRange; _firstPacket = firstPacket; _firstPacketLength = length; _local = new ProxySocketTunLocal(socket); _local.local_sendback_protocol = local_sendback_protocol; _config = config; _local_proxy = proxy; Connect(); } private void Connect() { try { IPAddress ipAddress = null; int _targetPort = 0; { if (_firstPacket[0] == 1) { byte[] addr = new byte[4]; Array.Copy(_firstPacket, 1, addr, 0, addr.Length); ipAddress = new IPAddress(addr); _targetPort = (_firstPacket[5] << 8) | _firstPacket[6]; _remote_host = ipAddress.ToString(); Logging.Info((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort.ToString()); } else if (_firstPacket[0] == 4) { byte[] addr = new byte[16]; Array.Copy(_firstPacket, 1, addr, 0, addr.Length); ipAddress = new IPAddress(addr); _targetPort = (_firstPacket[17] << 8) | _firstPacket[18]; _remote_host = ipAddress.ToString(); Logging.Info((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort.ToString()); } else if (_firstPacket[0] == 3) { int len = _firstPacket[1]; byte[] addr = new byte[len]; Array.Copy(_firstPacket, 2, addr, 0, addr.Length); _remote_host = Encoding.UTF8.GetString(_firstPacket, 2, len); _targetPort = (_firstPacket[len + 2] << 8) | _firstPacket[len + 3]; Logging.Info((_local_proxy ? "Local proxy" : "Direct") + " connect " + _remote_host + ":" + _targetPort.ToString()); //if (!_local_proxy) { if (!IPAddress.TryParse(_remote_host, out ipAddress)) { if (_config.proxyRuleMode == (int)ProxyRuleMode.UserCustom) { Shadowsocks.Model.HostMap hostMap = HostMap.Instance(); string host_addr; if (hostMap.GetHost(_remote_host, out host_addr)) { if (!String.IsNullOrEmpty(host_addr)) { string lower_host_addr = host_addr.ToLower(); if (lower_host_addr.StartsWith("reject")) { Close(); return; } else if (lower_host_addr.IndexOf('.') >= 0 || lower_host_addr.IndexOf(':') >= 0) { if (!IPAddress.TryParse(lower_host_addr, out ipAddress)) { // } } } } } if (ipAddress == null) { ipAddress = Utils.LocalDnsBuffer.Get(_remote_host); } } if (ipAddress == null) { if (_remote_host.IndexOf('.') >= 0) { ipAddress = Util.Utils.QueryDns(_remote_host, _config.localDnsServer); } else { ipAddress = Utils.QueryDns(_remote_host, null); } } if (ipAddress != null) { Utils.LocalDnsBuffer.Set(_remote_host, new IPAddress(ipAddress.GetAddressBytes())); Utils.LocalDnsBuffer.Sweep(); } else { if (!_local_proxy) throw new SocketException((int)SocketError.HostNotFound); } } } _remote_port = _targetPort; } if (ipAddress != null && _config.proxyRuleMode == (int)ProxyRuleMode.UserCustom) { Shadowsocks.Model.HostMap hostMap = HostMap.Instance(); string host_addr; if (hostMap.GetIP(ipAddress, out host_addr)) { string lower_host_addr = host_addr.ToLower(); if (lower_host_addr.StartsWith("reject") ) { Close(); return; } } } if (_local_proxy) { IPAddress.TryParse(_config.proxyHost, out ipAddress); _targetPort = _config.proxyPort; } // ProxyAuth recv only socks5 head, so don't need to save anything else IPEndPoint remoteEP = new IPEndPoint(ipAddress, _targetPort); _remote = new ProxySocketTun(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _remote.GetSocket().NoDelay = true; // Connect to the remote endpoint. _remote.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private bool ConnectProxyServer(string strRemoteHost, int iRemotePort) { if (_config.proxyType == 0) { bool ret = _remote.ConnectSocks5ProxyServer(strRemoteHost, iRemotePort, false, _config.proxyAuthUser, _config.proxyAuthPass); return ret; } else if (_config.proxyType == 1) { bool ret = _remote.ConnectHttpProxyServer(strRemoteHost, iRemotePort, _config.proxyAuthUser, _config.proxyAuthPass, _config.proxyUserAgent); return ret; } else { return true; } } private void ConnectCallback(IAsyncResult ar) { if (_closed) { return; } try { _remote.EndConnect(ar); if (_local_proxy) { if (!ConnectProxyServer(_remote_host, _remote_port)) { throw new SocketException((int)SocketError.ConnectionReset); } } StartPipe(); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void ResetTimeout(Double time) { if (time <= 0 && timer == null) return; if (time <= 0) { if (timer != null) { lock (timerLock) { if (timer != null) { timer.Enabled = false; timer.Elapsed -= timer_Elapsed; timer.Dispose(); timer = null; } } } } else { if (lastTimerSetTime != null && (DateTime.Now - lastTimerSetTime).TotalMilliseconds > 500) { lock (timerLock) { if (timer == null) { timer = new System.Timers.Timer(time * 1000.0); timer.Elapsed += timer_Elapsed; } else { timer.Interval = time * 1000.0; timer.Stop(); } timer.Start(); lastTimerSetTime = DateTime.Now; } } } } private void timer_Elapsed(object sender, ElapsedEventArgs e) { if (_closed) { return; } Close(); } private void StartPipe() { if (_closed) { return; } try { Server.GetForwardServerRef().GetConnections().AddRef(this); _remote.BeginReceive(remoteRecvBuffer, RecvSize, 0, new AsyncCallback(PipeRemoteReceiveCallback), null); _local.BeginReceive(connetionRecvBuffer, RecvSize, 0, new AsyncCallback(PipeConnectionReceiveCallback), null); _local.Send(connetionRecvBuffer, 0, 0); ResetTimeout(TTL); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void PipeRemoteReceiveCallback(IAsyncResult ar) { if (_closed) { return; } try { int bytesRead = _remote.EndReceive(ar); if (bytesRead > 0) { ResetTimeout(TTL); //_local.BeginSend(remoteRecvBuffer, bytesRead, 0, new AsyncCallback(PipeConnectionSendCallback), null); _local.Send(remoteRecvBuffer, bytesRead, 0); _totalRecvSize += bytesRead; if (_totalRecvSize <= 1024 * 1024 * 2) { _remote.BeginReceive(remoteRecvBuffer, RecvSize, 0, new AsyncCallback(PipeRemoteReceiveCallback), null); } else PipeRemoteReceiveLoop(); } else { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void PipeRemoteReceiveLoop() { bool final_close = false; byte[] recv_buffer = new byte[RecvSize]; DateTime beforeReceive = DateTime.Now; while (!_closed) { try { int bytesRead = _remote.Receive(recv_buffer, RecvSize, 0); DateTime now = DateTime.Now; if (_remote != null && _remote.IsClose) { final_close = true; break; } if (_closed) { break; } ResetTimeout(TTL); if (bytesRead > 0) { _local.Send(recv_buffer, bytesRead, 0); if ((now - beforeReceive).TotalSeconds > 5) { _totalRecvSize = 0; _remote.BeginReceive(remoteRecvBuffer, RecvSize, 0, new AsyncCallback(PipeRemoteReceiveCallback), null); return; } else { beforeReceive = now; } } else { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); final_close = true; break; } } if (final_close) Close(); } private void PipeConnectionReceiveCallback(IAsyncResult ar) { if (_closed) { return; } try { int bytesRead = _local.EndReceive(ar); if (bytesRead > 0) { ResetTimeout(TTL); //_remote.BeginSend(connetionRecvBuffer, bytesRead, 0, new AsyncCallback(PipeRemoteSendCallback), null); _remote.Send(connetionRecvBuffer, bytesRead, 0); _local.BeginReceive(connetionRecvBuffer, RecvSize, 0, new AsyncCallback(PipeConnectionReceiveCallback), null); } else { Close(); } } catch (Exception e) { Logging.LogUsefulException(e); Close(); } } private void CloseSocket(ProxySocketTun sock) { lock (this) { if (sock != null) { ProxySocketTun s = sock; try { s.Shutdown(SocketShutdown.Both); } catch { } try { s.Close(); } catch { } } } } public void Close() { lock (this) { if (_closed) { return; } _closed = true; } ResetTimeout(0); Thread.Sleep(100); CloseSocket(_remote); CloseSocket(_local); Server.GetForwardServerRef().GetConnections().DecRef(this); } public override void Shutdown() { InvokeHandler handler = () => Close(); handler.BeginInvoke(null, null); } } } } ================================================ FILE: shadowsocks-csharp/Controller/SpeedTest.cs ================================================ using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using Shadowsocks.Model; namespace Shadowsocks.Controller { class SpeedTester { #if DEBUG struct TransLog { public int dir; public int size; } #endif public DateTime timeConnectBegin; public DateTime timeConnectEnd; public DateTime timeBeginUpload; public DateTime timeBeginDownload; public long sizeUpload = 0; public long sizeDownload = 0; public long sizeProtocolRecv = 0; public long sizeRecv = 0; private List sizeTransfer = new List(); public string server; public ServerTransferTotal transfer; public int upload_cnt = 0; public int download_cnt = 0; public void BeginConnect() { timeConnectBegin = DateTime.Now; } public void EndConnect() { timeConnectEnd = DateTime.Now; } public void BeginUpload() { timeBeginUpload = DateTime.Now; } public bool BeginDownload() { if (timeBeginDownload == new DateTime()) { timeBeginDownload = DateTime.Now; return true; } return false; } public bool AddDownloadSize(int size) { //if (sizeDownloadList.Count == 2) // sizeDownloadList[1] = new TransLog(size, DateTime.Now); //else // sizeDownloadList.Add(new TransLog(size, DateTime.Now)); sizeDownload += size; if (transfer != null && server != null) { transfer.AddDownload(server, size); } upload_cnt = 0; download_cnt += 1; #if DEBUG if (sizeTransfer.Count < 1024 * 128) { lock (sizeTransfer) { sizeTransfer.Add(new TransLog { dir = 1, size = size }); } } #endif return download_cnt > 30; //return sizeDownload > 1024 * 256 && sizeDownload > (DateTime.Now - timeConnectEnd).TotalSeconds * 1024 * 16; } public void AddProtocolRecvSize(int size) { sizeProtocolRecv += size; } public void AddRecvSize(int size) { sizeRecv += size; } public bool AddUploadSize(int size) { sizeUpload += size; if (transfer != null && server != null) { transfer.AddUpload(server, size); } upload_cnt = 1; download_cnt = 0; #if DEBUG if (sizeTransfer.Count < 1024 * 128) { lock (sizeTransfer) { sizeTransfer.Add(new TransLog { dir = 0, size = size }); } } #endif return upload_cnt > 30; //return sizeUpload > 1024 * 256 && sizeUpload > (DateTime.Now - timeConnectEnd).TotalSeconds * 1024 * 16; } public string TransferLog() { string ret = ""; #if DEBUG int lastdir = -1; foreach (TransLog t in sizeTransfer) { if (t.dir != lastdir) { lastdir = t.dir; ret += (t.dir == 0 ? " u" : " d"); } ret += " " + t.size.ToString(); } #endif return ret; } } class ProtocolResponseDetector { public enum Protocol { UNKONWN = -1, NOTBEGIN = 0, HTTP = 1, TLS = 2, SOCKS4 = 4, SOCKS5 = 5, } protected Protocol protocol = Protocol.NOTBEGIN; protected byte[] send_buffer = new byte[0]; protected byte[] recv_buffer = new byte[0]; public bool Pass { get; set; } public ProtocolResponseDetector() { Pass = false; } public void OnSend(byte[] send_data, int length) { if (protocol != Protocol.NOTBEGIN) return; Array.Resize(ref send_buffer, send_buffer.Length + length); Array.Copy(send_data, 0, send_buffer, send_buffer.Length - length, length); if (send_buffer.Length < 2) return; int head_size = Obfs.ObfsBase.GetHeadSize(send_buffer, send_buffer.Length); if (send_buffer.Length - head_size < 0) return; byte[] data = new byte[send_buffer.Length - head_size]; Array.Copy(send_buffer, head_size, data, 0, data.Length); if (data.Length < 2) return; if (data.Length > 8) { if (data[0] == 22 && data[1] == 3 && (data[2] >= 0 && data[2] <= 3)) { protocol = Protocol.TLS; return; } if (data[0] == 'G' && data[1] == 'E' && data[2] == 'T' && data[3] == ' ' || data[0] == 'P' && data[1] == 'U' && data[2] == 'T' && data[3] == ' ' || data[0] == 'H' && data[1] == 'E' && data[2] == 'A' && data[3] == 'D' && data[4] == ' ' || data[0] == 'P' && data[1] == 'O' && data[2] == 'S' && data[3] == 'T' && data[4] == ' ' || data[0] == 'C' && data[1] == 'O' && data[2] == 'N' && data[3] == 'N' && data[4] == 'E' && data[5] == 'C' && data[6] == 'T' && data[7] == ' ' ) { protocol = Protocol.HTTP; return; } } else { protocol = Protocol.UNKONWN; } } public int OnRecv(byte[] recv_data, int length) { if (protocol == Protocol.UNKONWN || protocol == Protocol.NOTBEGIN) return 0; Array.Resize(ref recv_buffer, recv_buffer.Length + length); Array.Copy(recv_data, 0, recv_buffer, recv_buffer.Length - length, length); if (recv_buffer.Length < 2) return 0; if (protocol == Protocol.HTTP && recv_buffer.Length > 4) { if (recv_buffer[0] == 'H' && recv_buffer[1] == 'T' && recv_buffer[2] == 'T' && recv_buffer[3] == 'P') { Finish(); return 0; } else { protocol = Protocol.UNKONWN; return 1; //throw new ProtocolException("Wrong http response"); } } else if (protocol == Protocol.TLS && recv_buffer.Length > 4) { if (recv_buffer[0] == 22 && recv_buffer[1] == 3) { Finish(); return 0; } else { protocol = Protocol.UNKONWN; return 2; //throw new ProtocolException("Wrong tls response"); } } return 0; } protected void Finish() { send_buffer = null; recv_buffer = null; protocol = Protocol.UNKONWN; Pass = true; } } } ================================================ FILE: shadowsocks-csharp/Controller/SystemProxy.cs ================================================ 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; using System.ComponentModel; namespace Shadowsocks.Controller { public enum INTERNET_OPTION { // Sets or retrieves an INTERNET_PER_CONN_OPTION_LIST structure that specifies // a list of options for a particular connection. INTERNET_OPTION_PER_CONNECTION_OPTION = 75, // Notify the system that the registry settings have been changed so that // it verifies the settings on the next call to InternetConnect. INTERNET_OPTION_SETTINGS_CHANGED = 39, // Causes the proxy data to be reread from the registry for a handle. INTERNET_OPTION_REFRESH = 37, // Alerts the current WinInet instance that proxy settings have changed // and that they must update with the new settings. // To alert all available WinInet instances, set the Buffer parameter of // InternetSetOption to NULL and BufferLength to 0 when passing this option. INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95 } /// /// Constants used in INTERNET_PER_CONN_OPTION_OptionUnion struct. /// public enum INTERNET_PER_CONN_OptionEnum { INTERNET_PER_CONN_FLAGS = 1, INTERNET_PER_CONN_PROXY_SERVER = 2, INTERNET_PER_CONN_PROXY_BYPASS = 3, INTERNET_PER_CONN_AUTOCONFIG_URL = 4, INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5, INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL = 6, INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS = 7, INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME = 8, INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL = 9, INTERNET_PER_CONN_FLAGS_UI = 10 } /// /// Constants used in INTERNET_PER_CONN_OPTON struct. /// [Flags] public enum INTERNET_OPTION_PER_CONN_FLAGS { PROXY_TYPE_DIRECT = 0x00000001, // direct to net PROXY_TYPE_PROXY = 0x00000002, // via named proxy PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, // autoproxy URL PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection } /// /// Constants used in INTERNET_PER_CONN_OPTON struct. /// Windows 7 and later: /// Clients that support Internet Explorer 8 should query the connection type using INTERNET_PER_CONN_FLAGS_UI. /// If this query fails, then the system is running a previous version of Internet Explorer and the client should /// query again with INTERNET_PER_CONN_FLAGS. /// Restore the connection type using INTERNET_PER_CONN_FLAGS regardless of the version of Internet Explorer. /// XXX: If fails, notify user to upgrade Internet Explorer /// [Flags] public enum INTERNET_OPTION_PER_CONN_FLAGS_UI { PROXY_TYPE_DIRECT = 0x00000001, // direct to net PROXY_TYPE_PROXY = 0x00000002, // via named proxy PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, // autoproxy URL PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection } /// /// Used in INTERNET_PER_CONN_OPTION. /// When create a instance of OptionUnion, only one filed will be used. /// The StructLayout and FieldOffset attributes could help to decrease the struct size. /// [StructLayout(LayoutKind.Explicit)] public struct INTERNET_PER_CONN_OPTION_OptionUnion : IDisposable { // A value in INTERNET_OPTION_PER_CONN_FLAGS. [FieldOffset(0)] public int dwValue; [FieldOffset(0)] public System.IntPtr pszValue; [FieldOffset(0)] public System.Runtime.InteropServices.ComTypes.FILETIME ftValue; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (disposing) { if (pszValue != IntPtr.Zero) { Marshal.FreeHGlobal(pszValue); pszValue = IntPtr.Zero; } } } } [StructLayout(LayoutKind.Sequential)] public struct INTERNET_PER_CONN_OPTION { // A value in INTERNET_PER_CONN_OptionEnum. public int dwOption; public INTERNET_PER_CONN_OPTION_OptionUnion Value; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct INTERNET_PER_CONN_OPTION_LIST : IDisposable { public int Size; // The connection to be set. NULL means LAN. public System.IntPtr Connection; public int OptionCount; public int OptionError; // List of INTERNET_PER_CONN_OPTIONs. public System.IntPtr pOptions; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (disposing) { if (Connection != IntPtr.Zero) { Marshal.FreeHGlobal(Connection); Connection = IntPtr.Zero; } if (pOptions != IntPtr.Zero) { Marshal.FreeHGlobal(pOptions); pOptions = IntPtr.Zero; } } } } public static class WinINet { /// /// Set IE settings. /// private static void SetIEProxy(bool enable, bool global, string proxyServer, string pacURL, string connName) { List _optionlist = new List(); if (enable) { if (global) { // global proxy _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS_UI, Value = { dwValue = (int)(INTERNET_OPTION_PER_CONN_FLAGS_UI.PROXY_TYPE_PROXY //| INTERNET_OPTION_PER_CONN_FLAGS_UI.PROXY_TYPE_DIRECT ) } }); _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER, Value = { pszValue = Marshal.StringToHGlobalAuto(proxyServer) } }); _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS, Value = { pszValue = Marshal.StringToHGlobalAuto("") } }); } else { // pac _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS_UI, Value = { dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS_UI.PROXY_TYPE_AUTO_PROXY_URL } }); _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_AUTOCONFIG_URL, Value = { pszValue = Marshal.StringToHGlobalAuto(pacURL) } }); } } else { // direct _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS_UI, Value = { dwValue = (int)(INTERNET_OPTION_PER_CONN_FLAGS_UI.PROXY_TYPE_DIRECT //| INTERNET_OPTION_PER_CONN_FLAGS_UI.PROXY_TYPE_AUTO_DETECT ) } }); _optionlist.Add(new INTERNET_PER_CONN_OPTION { dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS, Value = { pszValue = Marshal.StringToHGlobalAuto("localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*;") } }); } // Get total length of INTERNET_PER_CONN_OPTIONs int len = 0; foreach(INTERNET_PER_CONN_OPTION option in _optionlist) { len += Marshal.SizeOf(option); } // Allocate a block of memory of the options. IntPtr buffer = Marshal.AllocCoTaskMem(len); IntPtr current = buffer; // Marshal data from a managed object to an unmanaged block of memory. foreach (INTERNET_PER_CONN_OPTION eachOption in _optionlist) { Marshal.StructureToPtr(eachOption, current, false); current = (IntPtr)((long)current + Marshal.SizeOf(eachOption)); } // Initialize a INTERNET_PER_CONN_OPTION_LIST instance. INTERNET_PER_CONN_OPTION_LIST optionList = new INTERNET_PER_CONN_OPTION_LIST(); // Point to the allocated memory. optionList.pOptions = buffer; // Return the unmanaged size of an object in bytes. optionList.Size = Marshal.SizeOf(optionList); optionList.Connection = String.IsNullOrEmpty(connName) ? IntPtr.Zero // NULL means LAN : Marshal.StringToHGlobalAuto(connName); // TODO: not working if contains Chinese optionList.OptionCount = _optionlist.Count; optionList.OptionError = 0; int optionListSize = Marshal.SizeOf(optionList); // Allocate memory for the INTERNET_PER_CONN_OPTION_LIST instance. IntPtr intptrStruct = Marshal.AllocCoTaskMem(optionListSize); // Marshal data from a managed object to an unmanaged block of memory. Marshal.StructureToPtr(optionList, intptrStruct, true); // Set internet settings. bool bReturn = NativeMethods.InternetSetOption( IntPtr.Zero, (int)INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intptrStruct, optionListSize); // Free the allocated memory. Marshal.FreeCoTaskMem(buffer); Marshal.FreeCoTaskMem(intptrStruct); // Throw an exception if this operation failed. if (!bReturn) { throw new Exception("InternetSetOption failed.", new Win32Exception()); } // Notify the system that the registry settings have been changed and cause // the proxy data to be reread from the registry for a handle. bReturn = NativeMethods.InternetSetOption( IntPtr.Zero, (int)INTERNET_OPTION.INTERNET_OPTION_PROXY_SETTINGS_CHANGED, IntPtr.Zero, 0); if (!bReturn) { Logging.Error("InternetSetOption:INTERNET_OPTION_PROXY_SETTINGS_CHANGED"); } bReturn = NativeMethods.InternetSetOption( IntPtr.Zero, (int)INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); if (!bReturn) { Logging.Error("InternetSetOption:INTERNET_OPTION_REFRESH"); } } public static void SetIEProxy(bool enable, bool global, string proxyServer, string pacURL) { string[] allConnections = null; var ret = RemoteAccessService.GetAllConns(ref allConnections); if (ret == 2) throw new Exception("Cannot get all connections"); if (ret == 1) { // no entries, only set LAN SetIEProxy(enable, global, proxyServer, pacURL, null); } else if (ret == 0) { // found entries, set LAN and each connection SetIEProxy(enable, global, proxyServer, pacURL, null); foreach (string connName in allConnections) { SetIEProxy(enable, global, proxyServer, pacURL, connName); } } } } internal static class RemoteAccessService { private enum RasFieldSizeConstants { #region original header //#if (WINVER >= 0x400) //#define RAS_MaxEntryName 256 //#define RAS_MaxDeviceName 128 //#define RAS_MaxCallbackNumber RAS_MaxPhoneNumber //#else //#define RAS_MaxEntryName 20 //#define RAS_MaxDeviceName 32 //#define RAS_MaxCallbackNumber 48 //#endif #endregion RAS_MaxEntryName = 256, RAS_MaxPath = 260 } private const int ERROR_SUCCESS = 0; private const int RASBASE = 600; private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct RasEntryName { #region original header //#define RASENTRYNAMEW struct tagRASENTRYNAMEW //RASENTRYNAMEW //{ // DWORD dwSize; // WCHAR szEntryName[RAS_MaxEntryName + 1]; // //#if (WINVER >= 0x500) // // // // If this flag is REN_AllUsers then its a // // system phonebook. // // // DWORD dwFlags; // WCHAR szPhonebookPath[MAX_PATH + 1]; //#endif //}; // //#define RASENTRYNAMEA struct tagRASENTRYNAMEA //RASENTRYNAMEA //{ // DWORD dwSize; // CHAR szEntryName[RAS_MaxEntryName + 1]; // //#if (WINVER >= 0x500) // DWORD dwFlags; // CHAR szPhonebookPath[MAX_PATH + 1]; //#endif //}; #endregion public int dwSize; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.RAS_MaxEntryName + 1)] public string szEntryName; public int dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)RasFieldSizeConstants.RAS_MaxPath + 1)] public string szPhonebookPath; } [DllImport("rasapi32.dll", CharSet = CharSet.Auto)] private static extern uint RasEnumEntries( // reserved, must be NULL string reserved, // pointer to full path and file name of phone-book file string lpszPhonebook, // buffer to receive phone-book entries [In, Out] RasEntryName[] lprasentryname, // size in bytes of buffer ref int lpcb, // number of entries written to buffer out int lpcEntries ); /// /// Get all entries from RAS /// /// /// /// 0: success with entries /// 1: success but no entries found /// 2: failed /// public static uint GetAllConns(ref string[] allConns) { int lpNames = 0; int entryNameSize = 0; int lpSize = 0; uint retval = ERROR_SUCCESS; RasEntryName[] names = null; entryNameSize = Marshal.SizeOf(typeof(RasEntryName)); // Windows Vista or later: To determine the required buffer size, call RasEnumEntries // with lprasentryname set to NULL. The variable pointed to by lpcb should be set to zero. // The function will return the required buffer size in lpcb and an error code of ERROR_BUFFER_TOO_SMALL. retval = RasEnumEntries(null, null, null, ref lpSize, out lpNames); if (retval == ERROR_BUFFER_TOO_SMALL) { names = new RasEntryName[lpNames]; for (int i = 0; i < names.Length; i++) { names[i].dwSize = entryNameSize; } retval = RasEnumEntries(null, null, names, ref lpSize, out lpNames); } if (retval == ERROR_SUCCESS) { if (lpNames == 0) { // no entries found. return 1; } allConns = new string[names.Length]; for (int i = 0; i < names.Length; i++) { allConns[i] = names[i].szEntryName; } return 0; } else { return 2; } } } public class SystemProxy { //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 = NativeMethods.InternetSetOption(IntPtr.Zero, (int)INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0); _refreshReturn = NativeMethods.InternetSetOption(IntPtr.Zero, (int)INTERNET_OPTION.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0); } public static void RegistrySetValue(RegistryKey registry, string name, object value) { try { registry.SetValue(name, value); } catch (Exception e) { Logging.LogUsefulException(e); } } public static RegistryKey OpenUserRegKey(string name, bool writable) { // we are building x86 binary for both x86 and x64, which will // cause problem when opening registry key // detect operating system instead of CPU #if _DOTNET_4_0 RegistryKey userKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "", Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32 ).OpenSubKey(name, writable); #else RegistryKey userKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, "" ).OpenSubKey(name, writable); #endif return userKey; } public static void Update(Configuration config, bool forceDisable) { int sysProxyMode = config.sysProxyMode; if (sysProxyMode == (int)ProxyMode.NoModify) { return; } if (forceDisable) { sysProxyMode = (int)ProxyMode.Direct; } bool global = sysProxyMode == (int)ProxyMode.Global; bool enabled = sysProxyMode != (int)ProxyMode.Direct; Version win8 = new Version("6.2"); //if (Environment.OSVersion.Version.CompareTo(win8) < 0) { using (RegistryKey registry = OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true)) { try { if (enabled) { if (global) { RegistrySetValue(registry, "ProxyEnable", 1); RegistrySetValue(registry, "ProxyServer", "127.0.0.1:" + config.localPort.ToString()); RegistrySetValue(registry, "AutoConfigURL", ""); } else { string pacUrl; pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?" + "auth=" + config.localAuthPassword + "&t=" + Util.Utils.GetTimestamp(DateTime.Now); RegistrySetValue(registry, "ProxyEnable", 0); RegistrySetValue(registry, "ProxyServer", ""); RegistrySetValue(registry, "AutoConfigURL", pacUrl); } } else { RegistrySetValue(registry, "ProxyEnable", 0); RegistrySetValue(registry, "ProxyServer", ""); RegistrySetValue(registry, "AutoConfigURL", ""); } IEProxyUpdate(config, sysProxyMode); 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")); } } } if (Environment.OSVersion.Version.CompareTo(win8) >= 0) { try { if (enabled) { if (global) { WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), ""); } else { string pacUrl; pacUrl = $"http://127.0.0.1:{config.localPort}/pac?auth={config.localAuthPassword}&t={Util.Utils.GetTimestamp(DateTime.Now)}"; WinINet.SetIEProxy(true, false, "", pacUrl); } } else { WinINet.SetIEProxy(false, false, "", ""); } } catch (Exception ex) { Logging.LogUsefulException(ex); } } } private static void CopyProxySettingFromLan() { using (RegistryKey registry = OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true)) { try { var defaultValue = registry.GetValue("DefaultConnectionSettings"); var connections = registry.GetValueNames(); foreach (String each in connections) { switch (each.ToUpperInvariant()) { case "DEFAULTCONNECTIONSETTINGS": case "SAVEDLEGACYSETTINGS": //case "LAN CONNECTION": continue; default: //set all the connections's proxy as the lan registry.SetValue(each, defaultValue); continue; } } SystemProxy.NotifyIE(); } catch (IOException e) { Logging.LogUsefulException(e); } } } private static void BytePushback(byte[] buffer, ref int buffer_len, int val) { BitConverter.GetBytes(val).CopyTo(buffer, buffer_len); buffer_len += 4; } private static void BytePushback(byte[] buffer, ref int buffer_len, string str) { BytePushback(buffer, ref buffer_len, str.Length); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); bytes.CopyTo(buffer, buffer_len); buffer_len += bytes.Length; } private static byte[] GenConnectionSettings(Configuration config, int sysProxyMode, int counter) { byte[] buffer = new byte[1024]; int buffer_len = 0; BytePushback(buffer, ref buffer_len, 70); BytePushback(buffer, ref buffer_len, counter + 1); if (sysProxyMode == (int)ProxyMode.Direct) BytePushback(buffer, ref buffer_len, 1); else if (sysProxyMode == (int)ProxyMode.Pac) BytePushback(buffer, ref buffer_len, 5); else BytePushback(buffer, ref buffer_len, 3); string proxy = "127.0.0.1:" + config.localPort.ToString(); BytePushback(buffer, ref buffer_len, proxy); string bypass = sysProxyMode == (int)ProxyMode.Global ? "" : "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*;"; BytePushback(buffer, ref buffer_len, bypass); string pacUrl = ""; pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?" + "auth=" + config.localAuthPassword + "&t=" + Util.Utils.GetTimestamp(DateTime.Now); BytePushback(buffer, ref buffer_len, pacUrl); buffer_len += 0x20; Array.Resize(ref buffer, buffer_len); return buffer; } /// /// Checks or unchecks the IE Options Connection setting of "Automatically detect Proxy" /// private static void IEProxyUpdate(Configuration config, int sysProxyMode) { using (RegistryKey registry = OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true)) { try { byte[] defConnection = (byte[])registry.GetValue("DefaultConnectionSettings"); int counter = 0; if (defConnection != null && defConnection.Length >= 8) { counter = defConnection[4] | (defConnection[5] << 8); } defConnection = GenConnectionSettings(config, sysProxyMode, counter); RegistrySetValue(registry, "DefaultConnectionSettings", defConnection); RegistrySetValue(registry, "SavedLegacySettings", defConnection); } catch (IOException e) { Logging.LogUsefulException(e); } } using (RegistryKey registry = OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true)) { try { RegistrySetValue(registry, "ProxyOverride", "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*;"); } catch (IOException e) { Logging.LogUsefulException(e); } } } } internal static class NativeMethods { [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); } } ================================================ FILE: shadowsocks-csharp/Controller/UpdateChecker.cs ================================================ using Shadowsocks.Model; using System; using System.Collections.Generic; using System.Net; using System.Text.RegularExpressions; using System.Xml; using System.Windows.Forms; namespace Shadowsocks.Controller { public class UpdateChecker { private const string UpdateURL = "https://raw.githubusercontent.com/Anankke/SSRR-Windows/master/shadowsocks-csharp/ssr-win-4.0.xml"; public string LatestVersionNumber; public string LatestVersionURL; public event EventHandler NewVersionFound; public const string Name = "ShadowsocksR"; public const string Copyright = "Copyright © BreakWa11 2017. Fork from Shadowsocks by clowwindy"; public const string Version = "5.1.5"; #if !_CONSOLE public const string NetVer = "4.0"; #else public const string NetVer = ""; #endif public const string FullVersion = Version + #if DEBUG " Debug"; #else /* " Alpha"; /*/ ""; //*/ #endif private static bool UseProxy = true; public void CheckUpdate(Configuration config) { try { WebClient http = new WebClient(); http.Headers.Add("User-Agent", String.IsNullOrEmpty(config.proxyUserAgent) ? "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36" : config.proxyUserAgent); if (UseProxy) { WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); if (!string.IsNullOrEmpty(config.authPass)) { proxy.Credentials = new NetworkCredential(config.authUser, config.authPass); } http.Proxy = proxy; } else { http.Proxy = null; } http.DownloadStringCompleted += http_DownloadStringCompleted; http.DownloadStringAsync(new Uri(UpdateURL + "?rnd=" + Util.Utils.RandUInt32().ToString())); } catch (Exception e) { Logging.LogUsefulException(e); } } 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 { // 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, @".*" + Name + @"-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase); if (match.Success) { if (match.Groups.Count == 2) { return match.Groups[1].Value; } } return null; } private void SortVersions(List 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; if (url.IndexOf("banned") > 0 && CompareVersion(version, currentVersion) == 0 || url.IndexOf("deprecated") > 0 && CompareVersion(version, currentVersion) > 0) { Application.Exit(); return false; } 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 versions = new List(); 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); NewVersionFound?.Invoke(this, new EventArgs()); } catch (Exception ex) { if (e.Error != null) { Logging.Debug(e.Error.ToString()); } Logging.Debug(ex.ToString()); NewVersionFound?.Invoke(this, new EventArgs()); return; } } } } ================================================ FILE: shadowsocks-csharp/Controller/UpdateFreeNode.cs ================================================ 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; using System.Windows.Forms; namespace Shadowsocks.Controller { public class UpdateFreeNode { private const string UpdateURL = "https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/free/freenodeplain.txt"; public event EventHandler NewFreeNodeFound; public string FreeNodeResult; public ServerSubscribe subscribeTask; public bool noitify; public const string Name = "ShadowsocksR"; public void CheckUpdate(Configuration config, ServerSubscribe subscribeTask, bool use_proxy, bool noitify) { FreeNodeResult = null; this.noitify = noitify; try { WebClient http = new WebClient(); http.Headers.Add("User-Agent", String.IsNullOrEmpty(config.proxyUserAgent) ? "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36" : config.proxyUserAgent); http.QueryString["rnd"] = Util.Utils.RandUInt32().ToString(); if (use_proxy) { WebProxy proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort); if (!string.IsNullOrEmpty(config.authPass)) { proxy.Credentials = new NetworkCredential(config.authUser, config.authPass); } http.Proxy = proxy; } else { http.Proxy = null; } //UseProxy = !UseProxy; this.subscribeTask = subscribeTask; string URL = subscribeTask.URL; http.DownloadStringCompleted += http_DownloadStringCompleted; http.DownloadStringAsync(new Uri(URL != null ? URL : UpdateURL)); } catch (Exception e) { Logging.LogUsefulException(e); } } private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { try { string response = e.Result; FreeNodeResult = response; if (NewFreeNodeFound != null) { NewFreeNodeFound(this, new EventArgs()); } } catch (Exception ex) { if (e.Error != null) { Logging.Debug(e.Error.ToString()); } Logging.Debug(ex.ToString()); if (NewFreeNodeFound != null) { NewFreeNodeFound(this, new EventArgs()); } return; } } } public class UpdateSubscribeManager { private Configuration _config; private List _serverSubscribes; private UpdateFreeNode _updater; private string _URL; private bool _use_proxy; public bool _noitify; public void CreateTask(Configuration config, UpdateFreeNode updater, int index, bool use_proxy, bool noitify) { if (_config == null) { _config = config; _updater = updater; _use_proxy = use_proxy; _noitify = noitify; if (index < 0) { _serverSubscribes = new List(); for (int i = 0; i < config.serverSubscribes.Count; ++i) { _serverSubscribes.Add(config.serverSubscribes[i]); } } else if (index < _config.serverSubscribes.Count) { _serverSubscribes = new List(); _serverSubscribes.Add(config.serverSubscribes[index]); } Next(); } } public bool Next() { if (_serverSubscribes.Count == 0) { _config = null; return false; } else { _URL = _serverSubscribes[0].URL; _updater.CheckUpdate(_config, _serverSubscribes[0], _use_proxy, _noitify); _serverSubscribes.RemoveAt(0); return true; } } public string URL { get { return _URL; } } } } ================================================ FILE: shadowsocks-csharp/Data/cn.txt ================================================ # translation for Simplified Chinese Shadowsocks=Shadowsocks # Menu items Mode=系统代理模式 No modify system proxy=保持当前状态不修改 Disable system proxy=直连模式 PAC=PAC 模式 Global=全局模式 Proxy rule=代理规则 Bypass LAN=绕过局域网 Bypass LAN && China=绕过局域网和大陆 Bypass LAN && not China=绕过局域网和非大陆 User custom=用户自定义 Disable bypass=全局 Servers=服务器 Edit servers...=编辑服务器... Import servers from file...=从文件导入... Import from clipboard SSR links...=从剪贴板SSR链接导入... Import from screen QRCode...=从屏幕二维码导入... Servers Subscribe=服务器订阅 Subscribe setting...=SSR服务器订阅设置... Update subscribe SSR node=更新SSR服务器订阅 Update subscribe SSR node(use proxy)=更新SSR服务器订阅(通过代理) Global settings...=选项设置... Start on Boot=开机启动 Allow Clients from LAN=允许来自局域网的连接 Load balance=服务器负载均衡 Same host for same address=优先相同节点连接同一地址 Enable domain white list(http proxy only)=使用域名白名单(仅http代理) Update local PAC from Lan IP list=更新PAC为绕过局域网IP Update local PAC from Chn White list=更新PAC为绕过大陆常见域名列表 Update local PAC from Chn IP list=更新PAC为绕过大陆IP(慎用) Update local PAC from GFWList=更新PAC为GFWList Update local PAC from Chn Only list=更新PAC为仅通过大陆常见域名(国外访问大陆) Copy PAC URL=复制 PAC URL Edit local PAC file...=编辑本地 PAC 文件... Edit user rule for GFWList...=编辑 GFWList 的用户规则... Show QRCode...=显示二维码... Port settings...=端口设置... Server statistic...=服务器连接统计... Disconnect current=断开当前所有连接 New version {0} {1} available=【点击下载新版本 {0} {1}】 Help=帮助 Check update=检查更新 Show logs...=显示日志... Open wiki...=打开Wiki文档... Feedback...=问题反馈... Gen custom QRCode...=自定义生成二维码 Reset password...=设置客户端密码... About...=关于... Donate...=捐助... Quit=退出 Edit Servers=编辑服务器 Global Settings=选项设置 # Config Form &Add=添加(&A) &Delete=删除(&D) Up=上移 Down=下移 New server=未配置的服务器 Server=服务器(截图打码) Server IP=服务器 IP Server Port=服务器端口 UDP Port=UDP端口 Password=密码 Encryption=加密 Remarks=备注 Adv. Setting=高级选项 Obfs UDP=混淆UDP协议 NOT all server support belows=以下选项不是所有服务端都支持 TCP over TCP if not checked=不打钩使用原生 TCP(暂不能用) UDP over UDP if not checked=不打钩使用原生 UDP Recommend checked=保留功能 Protocol=协议 Protocol param=协议参数 Obfs=混淆 Obfs param=混淆参数 SSR Link=SSR链接 Original=原版 Verify all=校验所有数据 Balance=负载均衡 OneByOne=按次序 Random=随机 FastDownloadSpeed=下载速度优先 LowLatency=低延迟优先 LowException=低错误优先 SelectedFirst=选中优先 Timer=定时切换 Balance in group=所选组切换 AutoBan=自动禁用出错服务器 Remote proxy=二级(前置)代理 Proxy On=开启代理 PAC "direct" return this proxy=PAC“直连”使用二级代理 Socks5(support UDP)=Socks5(支持UDP) Http tunnel=Http隧道 TCP Port tunnel=TCP端口转发(需要相关混淆插件) Username=用户名 Local proxy=本地代理 Build-in http proxy=内置http代理(目前有bug) Proxy Port=本地端口 Reconnect Times=重连次数 Timeout=连接超时 TTL=空闲断开秒数 OK=确定 Cancel=取消 # ServerLog Form ServerLog=服务器记录 &Control=操作(&C) &Disconnect direct connections=断开直连连接(&D) Disconnect &All=断开当前所有连接(&A) Clear &MaxSpeed=重置历史最高(&M) &Clear=清空(&C) Clear &Selected Total=清空选中节点历史流量(&S) Clear &Total=清空所有历史流量(&T) Port &out=导出(&O) Copy current link=复制选中链接 Copy current group links=复制选中组链接 Copy all enable links=复制所有开启节点链接 Copy all links=复制所有节点链接 &Window=窗口(&W) Auto &size=自动调整大小(&S) Always On &Top=窗口置顶(&T) Total Connect=总连接 Enable=开关 Connecting=连接 Latency=延迟 Avg DSpeed=下载 Max DSpeed=最高 Avg UpSpeed=上传 Max UpSpeed=最高 Upload=总上传 Dload=总下载 DloadRaw=实下载 Error=错误 Timeout=超时 Empty Response=空连 Error Percent=出错比例 Continuous=连错 Version=版本 # Global Log Form &File=文件(&F) Clear &log=清空日志(&L) Show in &Explorer=在资源管理器中显示(&E) &Close=关闭(&C) &View=视图(&V) &Font...=字体设置(&F)... &Wrap Text=自动换行(&W) &Always on top=置于顶层(&A) Log Viewer=日志查看器 # QRCode Form QRCode=二维码 # PAC Url Form Edit Online PAC URL=编辑在线 PAC 网址 Edit Online PAC URL...=编辑在线 PAC 网址... Please input PAC Url=请输入 PAC 网址 # InputPassword Form InputPassword=输入密码 Parse gui-config.json error, maybe require password to decrypt=解析 gui-config.json 出错, 可能需要密码解密 # ResetPassword Form ResetPassword=重置密码 This password is use to encrypt local SSR data.=本密码用于加密SSR本地数据 Old Password=旧密码 New Password=新密码 Confirm Password=确认密码 # PortSettingsForm Form Port Settings=端口设置 Map Setting=映射设置 Type=类型 Server ID=服务器 ID Target Addr=目标地址 Target Port=目标端口 Local Port=本地端口 Port Forward=端口转发 Force Proxy=强制代理 Proxy With Rule=规则代理 #Enable=启用 #Remarks=备注 #OK=确定 #Cancel= #&Add= #&Delete= # SubscribeForm Form Subscribe Settings=订阅设置 URL=网址 Group name=组名 Last Update=最近更新 Auto update=自动更新 # Messages Shadowsocks Error: {0}=Shadowsocks 错误: {0} Port already in use=端口已被占用 Illegal port number format=非法端口格式 Please add at least one server=请添加至少一个服务器 Server IP can not be blank=服务器 IP 不能为空 Password can not be blank=密码不能为空 Password are blank=密码为空 Port out of range=端口超出范围 {0} {1} Update Found={0} {1} 更新 Click menu to download=点击菜单项下载 ShadowsocksR is here=ShadowsocksR 在这里 You can turn on/off ShadowsocksR in the context menu=可以在右键菜单中开关 ShadowsocksR System Proxy Enabled=系统代理已启用 System Proxy Disabled=系统代理未启用 Failed to update PAC file =更新 PAC 文件失败 PAC updated=更新 PAC 成功 Domain white list list updated=更新域名白名单成功 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.=未发现二维码,尝试把它放大或移动到靠近屏幕中间的位置 ShadowsocksR is already running.=ShadowsocksR 已经在运行。 Find Shadowsocks icon in your notify tray.=请在任务栏里寻找 ShadowsocksR 图标。 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} Password NOT match=密码不匹配 Update subscribe {0} success=服务器订阅 {0} 更新成功 Update subscribe {0} failure=服务器订阅 {0} 更新失败 Success=成功 ================================================ FILE: shadowsocks-csharp/Data/privoxy_conf.txt ================================================ listen-address __PRIVOXY_BIND_IP__:__PRIVOXY_BIND_PORT__ show-on-task-bar 0 activity-animation 0 forward-socks5 / 127.0.0.1:__SOCKS_PORT__ . hide-console __BYPASS_ACTION__ ================================================ FILE: shadowsocks-csharp/Data/user-rule.txt ================================================ ! Put user rules line by line in this file. ! See https://adblockplus.org/en/filter-cheatsheet ================================================ FILE: shadowsocks-csharp/Data/zh-tw.txt ================================================ # translation for Traditional Chinese Shadowsocks=Shadowsocks # Menu items Mode=系統代理模式 No modify system proxy=保持當前狀態不修改 Disable system proxy=直連模式 PAC=PAC 模式 Global=全局模式 Proxy rule=代理規則 Bypass LAN=繞過區域網路 Bypass LAN && China=繞過區域網路和大陸 Bypass LAN && not China=繞過區域網路和非大陸 User custom=用戶自定義 Disable bypass=全局 Servers=伺服器 Edit servers...=編輯伺服器... Import servers from file...=從文件導入... Import from clipboard SSR links...=从剪貼板SSR連結導入... Import from screen QRCode...=从熒幕 QR 碼導入... Servers Subscribe=伺服器訂閱 Subscribe setting...=SSR伺服器訂閱設置... Update subscribe SSR node=更新SSR伺服器訂閱 Update subscribe SSR node(use proxy)=更新SSR伺服器訂閱(通過代理) Global settings...=選項設置... Start on Boot=開機啟動 Allow Clients from LAN=允許來自區域網路的連接 Load balance=伺服器負載均衡 Same host for same address=優先相同節點連接同一位址 Enable domain white list(http proxy only)=使用域名白名單(僅http代理) Update local PAC from Lan IP list=更新PAC為繞過區域網路IP Update local PAC from Chn White list=更新PAC為繞過大陸常見域名列表 Update local PAC from Chn IP list=更新PAC為繞過大陸IP(慎用) Update local PAC from GFWList=更新PAC為GFWList Update local PAC from Chn Only list=更新PAC為僅通過大陸常見域名(國外訪問大陸) Copy PAC URL=複製 PAC URL Edit local PAC file...=編輯本地 PAC 文件... Edit user rule for GFWList...=編輯 GFWList 的用戶規則... Show QRCode...=顯示 QR 碼... Port settings...=連接埠設置... Server statistic...=伺服器連接統計... Disconnect current=斷開當前所有連接 New version {0} {1} available=【點擊下載新版本 {0} {1}】 Help=幫助 Check update=檢查更新 Show logs...=顯示日誌... Open wiki...=打開Wiki文檔... Feedback...=問題反饋... Gen custom QRCode...=自定義生成 QR 碼 Reset password...=設置客戶端密碼... About...=關於... Donate...=捐助... Quit=退出 Edit Servers=編輯伺服器 Global Settings=選項設置 # Config Form &Add=添加(&A) &Delete=刪除(&D) Up=上移 Down=下移 New server=未配置的伺服器 Server=伺服器(截圖打碼) Server IP=伺服器 IP Server Port=伺服器連接埠 UDP Port=UDP連接埠 Password=密碼 Encryption=加密 Remarks=備註 Adv. Setting=高級選項 Obfs UDP=混淆UDP協議 NOT all server support belows=以下選項不是所有伺服器都支持 TCP over TCP if not checked=不打勾使用原生 TCP(暫不能用) UDP over UDP if not checked=不打勾使用原生 UDP Recommend checked=保留功能 Protocol=協議 Protocol param=協議參數 Obfs=混淆 Obfs param=混淆參數 SSR Link=SSR連結 Original=原版 Verify all=校驗所有數據 Balance=負載均衡 OneByOne=按次序 Random=隨機 FastDownloadSpeed=下載速度優先 LowLatency=低延遲優先 LowException=低錯誤優先 SelectedFirst=選中優先 Timer=定時切換 Balance in group=所選組切換 AutoBan=自動禁用出錯伺服器 Remote proxy=二級(前置)代理 Proxy On=開啟代理 PAC "direct" return this proxy=PAC“直接連接”使用二級代理 Socks5(support UDP)=Socks5(支持UDP) Http tunnel=Http隧道 TCP Port tunnel=TCP連接埠轉發(需要相關混淆插件) Username=用戶名 Local proxy=本地代理 Build-in http proxy=內置http代理(目前有bug) Proxy Port=本地連接埠 Reconnect Times=重連次數 Timeout=連接超時 TTL=空閒斷開秒數 OK=確定 Cancel=取消 # ServerLog Form ServerLog=伺服器記錄 &Control=操作(&C) &Disconnect direct connections=斷開直連連接(&D) Disconnect &All=斷開當前所有連接(&A) Clear &MaxSpeed=重置歷史最高(&M) &Clear=清空(&C) Clear &Selected Total=清空選中節點歷史流量(&S) Clear &Total=清空所有歷史流量(&T) Port &out=導出(&O) Copy current link=複製選中連結 Copy current group links=複製選中組連結 Copy all enable links=複製所有開啟節點連結 Copy all links=複製所有節點連結 &Window=窗口(&W) Auto &size=自動調整大小(&S) Always On &Top=窗口置頂(&T) Total Connect=總連接 Enable=開關 Connecting=連接 Latency=延遲 Avg DSpeed=下載 Max DSpeed=最高 Avg UpSpeed=上傳 Max UpSpeed=最高 Upload=總上傳 Dload=總下載 DloadRaw=實下載 Error=錯誤 Timeout=超時 Empty Response=空連 Error Percent=出錯比例 Continuous=連錯 Version=版本 # QRCode Form QRCode=QR 碼 # PAC Url Form Edit Online PAC URL=編輯在線 PAC 網址 Edit Online PAC URL...=編輯在線 PAC 網址... Please input PAC Url=請輸入 PAC 網址 # InputPassword Form InputPassword=輸入密碼 Parse gui-config.json error, maybe require password to decrypt=解析 gui-config.json 出錯, 可能需要密碼解密 # ResetPassword Form ResetPassword=重設密碼 This password is use to encrypt local SSR data.=本密碼用於加密 SSR 本地數據 Old Password=舊密碼 New Password=新密碼 Confirm Password=確認密碼 # PortSettingsForm Form Port Settings=連接埠設置 Map Setting=映射設置 Type=類型 Server ID=伺服器 ID Target Addr=目標地址 Target Port=目標連接埠 Local Port=本地連接埠 Port Forward=連接埠轉發 Force Proxy=強制代理 Proxy With Rule=規則代理 #Enable= #Remarks= #OK= #Cancel= #&Add= #&Delete= # SubscribeForm Form Subscribe Settings=訂閱設置 URL=網址 Group name=組名 Last Update=最近更新 Auto update=自動更新 # Messages Shadowsocks Error: {0}=Shadowsocks 錯誤: {0} Port already in use=連接埠已被占用 Illegal port number format=非法連接埠格式 Please add at least one server=請添加至少一個伺服器 Server IP can not be blank=伺服器 IP 不能為空 Password can not be blank=密碼不能為空 Password are blank=密碼為空 Port out of range=連接埠超出範圍 {0} {1} Update Found={0} {1} 更新 Click menu to download=點擊菜單項下載 ShadowsocksR is here=ShadowsocksR 在這裡 You can turn on/off ShadowsocksR in the context menu=可以在右鍵菜單中開關 ShadowsocksR System Proxy Enabled=系統代理已啟用 System Proxy Disabled=系統代理未啟用 Failed to update PAC file =更新 PAC 文件失敗 PAC updated=更新 PAC 成功 Domain white list list updated=更新域名白名單成功 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.=未發現 QR 碼,嘗試把它放大或移動到靠近屏幕中間的位置 ShadowsocksR is already running.=ShadowsocksR 已經在運行。 Find Shadowsocks icon in your notify tray.=請在工作列裡尋找 ShadowsocksR 圖標。 If you want to start multiple Shadowsocks, make a copy in another directory.=如果想啟動多份,可以另外複製一份到別的目錄。 Failed to decode QRCode=無法解析 QR 碼 Failed to update registry=無法修改註冊表 System Proxy On: =系統代理已啟用: Running: Port {0}=正在運行:連接埠 {0} Password NOT match=密碼不匹配 Update subscribe {0} success=伺服器訂閱 {0} 更新成功 Update subscribe {0} failure=伺服器訂閱 {0} 更新失敗 Success=成功 ================================================ FILE: shadowsocks-csharp/Encryption/EncryptorBase.cs ================================================ using System.Security.Cryptography; using System.Text; namespace Shadowsocks.Encryption { public struct EncryptorInfo { public int key_size; public int iv_size; public bool display; public int type; public string name; public EncryptorInfo(int key, int iv, bool display, int type, string name = "") { key_size = key; iv_size = iv; this.display = display; this.type = type; this.name = name; } } public abstract class EncryptorBase : IEncryptor { public const int MAX_INPUT_SIZE = 65536; protected EncryptorBase(string method, string password) { Method = method; Password = password; } protected string Method; protected string Password; protected byte[] GetPasswordHash() { byte[] inputBytes = Encoding.UTF8.GetBytes(Password); byte[] hash = MbedTLS.MD5(inputBytes); return hash; } public abstract bool SetIV(byte[] iv); public abstract void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength); public abstract void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength); public abstract void ResetEncrypt(); public abstract void ResetDecrypt(); public abstract void Dispose(); public abstract byte[] getIV(); public abstract byte[] getKey(); public abstract EncryptorInfo getInfo(); } } ================================================ FILE: shadowsocks-csharp/Encryption/EncryptorFactory.cs ================================================  using System; using System.Collections.Generic; using System.Reflection; namespace Shadowsocks.Encryption { public static class EncryptorFactory { private static Dictionary _registeredEncryptors; private static List _registeredEncryptorNames; private static Type[] _constructorTypes = new Type[] { typeof(string), typeof(string), typeof(bool) }; static EncryptorFactory() { _registeredEncryptors = new Dictionary(); _registeredEncryptorNames = new List(); foreach (string method in NoneEncryptor.SupportedCiphers()) { if (!_registeredEncryptorNames.Contains(method)) { _registeredEncryptorNames.Add(method); _registeredEncryptors.Add(method, typeof(NoneEncryptor)); } } { foreach (string method in MbedTLSEncryptor.SupportedCiphers()) { if (!_registeredEncryptorNames.Contains(method)) { _registeredEncryptorNames.Add(method); _registeredEncryptors.Add(method, typeof(MbedTLSEncryptor)); } } } if (LibcryptoEncryptor.isSupport()) { LibcryptoEncryptor.InitAviable(); foreach (string method in LibcryptoEncryptor.SupportedCiphers()) { if (!_registeredEncryptorNames.Contains(method)) { _registeredEncryptorNames.Add(method); _registeredEncryptors.Add(method, typeof(LibcryptoEncryptor)); } } } foreach (string method in SodiumEncryptor.SupportedCiphers()) { if (!_registeredEncryptorNames.Contains(method)) { _registeredEncryptorNames.Add(method); _registeredEncryptors.Add(method, typeof(SodiumEncryptor)); } } } public static List GetEncryptor() { return _registeredEncryptorNames; } public static IEncryptor GetEncryptor(string method, string password, bool cache) { if (string.IsNullOrEmpty(method)) { method = "aes-256-cfb"; } method = method.ToLowerInvariant(); Type t = _registeredEncryptors[method]; ConstructorInfo c = t.GetConstructor(_constructorTypes); IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, password, cache }); return result; } public static EncryptorInfo GetEncryptorInfo(string method) { if (string.IsNullOrEmpty(method)) { method = "aes-256-cfb"; } method = method.ToLowerInvariant(); Type t = _registeredEncryptors[method]; ConstructorInfo c = t.GetConstructor(_constructorTypes); IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, "0", false }); EncryptorInfo info = result.getInfo(); result.Dispose(); return info; } } } ================================================ FILE: shadowsocks-csharp/Encryption/IEncryptor.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Encryption { public interface IEncryptor : IDisposable { bool SetIV(byte[] iv); void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength); void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength); void ResetEncrypt(); void ResetDecrypt(); byte[] getIV(); byte[] getKey(); EncryptorInfo getInfo(); } } ================================================ FILE: shadowsocks-csharp/Encryption/IVEncryptor.cs ================================================ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using Shadowsocks.Model; namespace Shadowsocks.Encryption { public abstract class IVEncryptor : EncryptorBase { protected Dictionary ciphers; private static readonly LRUCache CachedKeys = new LRUCache(600); protected byte[] _encryptIV; protected byte[] _decryptIV; protected int _decryptIVReceived; protected bool _encryptIVSent; protected int _encryptIVOffset = 0; protected int _decryptIVOffset = 0; protected string _method; protected int _cipher; protected EncryptorInfo _cipherInfo; protected byte[] _key; protected int keyLen; protected byte[] _iv; protected int ivLen; protected byte[] encbuf = new byte[MAX_INPUT_SIZE]; protected byte[] decbuf = new byte[MAX_INPUT_SIZE]; public IVEncryptor(string method, string password, bool cache) : base(method, password) { InitKey(method, password); } protected abstract Dictionary getCiphers(); public override bool SetIV(byte[] iv) { if (iv != null && iv.Length == ivLen) { iv.CopyTo(_iv, 0); _encryptIVSent = true; initCipher(iv, true); return true; } return false; } public override byte[] getIV() { return _iv; } public override byte[] getKey() { byte[] key = (byte[])_key.Clone(); Array.Resize(ref key, keyLen); return key; } public override EncryptorInfo getInfo() { return _cipherInfo; } protected void InitKey(string method, string password) { method = method.ToLower(); _method = method; string k = method + ":" + password; ciphers = getCiphers(); _cipherInfo = ciphers[_method]; _cipher = _cipherInfo.type; if (_cipher == 0) { throw new Exception("method not found"); } keyLen = ciphers[_method].key_size; ivLen = ciphers[_method].iv_size; if (!CachedKeys.ContainsKey(k)) { lock (CachedKeys) { if (!CachedKeys.ContainsKey(k)) { byte[] passbuf = Encoding.UTF8.GetBytes(password); _key = new byte[32]; byte[] iv = new byte[16]; bytesToKey(passbuf, _key); CachedKeys.Set(k, _key); CachedKeys.Sweep(); } } } if (_key == null) _key = CachedKeys.Get(k); Array.Resize(ref _iv, ivLen); randBytes(_iv, ivLen); } protected void bytesToKey(byte[] password, byte[] key) { byte[] result = new byte[password.Length + 16]; int i = 0; byte[] md5sum = null; while (i < key.Length) { if (i == 0) { md5sum = MbedTLS.MD5(password); } else { md5sum.CopyTo(result, 0); password.CopyTo(result, md5sum.Length); md5sum = MbedTLS.MD5(result); } md5sum.CopyTo(key, i); i += md5sum.Length; } } protected static void randBytes(byte[] buf, int length) { byte[] temp = new byte[length]; RNGCryptoServiceProvider rngServiceProvider = new RNGCryptoServiceProvider(); rngServiceProvider.GetBytes(temp); temp.CopyTo(buf, 0); } protected virtual void initCipher(byte[] iv, bool isCipher) { if (ivLen > 0) { if (isCipher) { _encryptIV = new byte[ivLen]; Array.Copy(iv, _encryptIV, ivLen); } else { _decryptIV = new byte[ivLen]; Array.Copy(iv, _decryptIV, ivLen); } } } protected abstract void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf); public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { if (!_encryptIVSent) { _encryptIVSent = true; Buffer.BlockCopy(_iv, 0, outbuf, 0, ivLen); initCipher(outbuf, true); outlength = length + ivLen; cipherUpdate(true, length, buf, encbuf); Buffer.BlockCopy(encbuf, 0, outbuf, ivLen, length); } else { outlength = length; cipherUpdate(true, length, buf, outbuf); } } public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { if (_decryptIVReceived <= ivLen) { int start_pos = ivLen; if (_decryptIVReceived + length < ivLen) { if (_decryptIV == null) { _decryptIV = new byte[ivLen]; } Buffer.BlockCopy(buf, 0, _decryptIV, _decryptIVReceived, length); outlength = 0; _decryptIVReceived += length; } else if (_decryptIVReceived == 0) { initCipher(buf, false); outlength = length - ivLen; _decryptIVReceived = ivLen; } else { start_pos = ivLen - _decryptIVReceived; byte[] temp_buf = new byte[ivLen]; Buffer.BlockCopy(_decryptIV, 0, temp_buf, 0, _decryptIVReceived); Buffer.BlockCopy(buf, 0, temp_buf, _decryptIVReceived, start_pos); initCipher(temp_buf, false); outlength = length - start_pos; _decryptIVReceived = ivLen; } if (outlength > 0) { _decryptIVReceived += outlength; Buffer.BlockCopy(buf, start_pos, decbuf, 0, outlength); cipherUpdate(false, outlength, decbuf, outbuf); } } else { outlength = length; cipherUpdate(false, length, buf, outbuf); } } public override void ResetEncrypt() { _encryptIVSent = false; _encryptIVOffset = 0; // SSL randBytes(_iv, ivLen); } public override void ResetDecrypt() { _decryptIVReceived = 0; _decryptIVOffset = 0; // SSL } } public class NoneEncryptor : IVEncryptor { public NoneEncryptor(string method, string password, bool cache) : base(method, password, cache) { InitKey(method, password); } private static Dictionary _ciphers = new Dictionary { {"none", new EncryptorInfo(16, 0, true, 1)}, }; public static List SupportedCiphers() { return new List(_ciphers.Keys); } protected override Dictionary getCiphers() { return _ciphers; } protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) { if (_disposed) { throw new ObjectDisposedException(this.ToString()); } Array.Copy(buf, outbuf, length); } #region IDisposable private bool _disposed; public override void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~NoneEncryptor() { Dispose(false); } protected virtual void Dispose(bool disposing) { lock (this) { if (_disposed) { return; } _disposed = true; } } #endregion } } ================================================ FILE: shadowsocks-csharp/Encryption/Libcrypto.cs ================================================ using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; using Shadowsocks.Controller; using Shadowsocks.Properties; namespace Shadowsocks.Encryption { class Libcrypto { delegate IntPtr EncryptFunc(); const string DLLNAME = "libeay32"; static Dictionary encrypt_func_map; static Libcrypto() { try { //try //{ // dlopen("libcrypto.so", 2); // return; //} //catch (Exception e) //{ // //Console.WriteLine(e.ToString()); //} string runningPath = Path.Combine(System.Windows.Forms.Application.StartupPath, @"temp"); // Path.GetTempPath(); if (!Directory.Exists(runningPath)) { Directory.CreateDirectory(runningPath); } string dllPath = Path.Combine(runningPath, "libeay32.dll"); try { //FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll); LoadLibrary(dllPath); } catch (IOException) { } catch //(Exception e) { //Console.WriteLine(e.ToString()); } } finally { if (encrypt_func_map == null && isSupport()) { Dictionary func_map = new Dictionary(); func_map["rc4"] = EVP_rc4; func_map["aes-128-cfb"] = EVP_aes_128_cfb; encrypt_func_map = func_map; OpenSSL_add_all_ciphers(); } } } public static bool isSupport() { try { IntPtr cipher = EVP_get_cipherbyname(null); return true; } catch { return false; } } public static bool is_cipher(string cipher_name) { string real_cipher_name = cipher_name; if (cipher_name.StartsWith("rc4-md5")) { real_cipher_name = "rc4"; } IntPtr ctx = IntPtr.Zero; byte[] cipher_name_buf = Encoding.ASCII.GetBytes(real_cipher_name); Array.Resize(ref cipher_name_buf, cipher_name_buf.Length + 1); IntPtr cipher = EVP_get_cipherbyname(cipher_name_buf); return cipher != IntPtr.Zero; } public static IntPtr init(string cipher_name, byte[] key, byte[] iv, int op) { IntPtr ctx = IntPtr.Zero; string real_cipher_name = cipher_name; if (cipher_name.StartsWith("rc4-md5")) { real_cipher_name = "rc4"; } byte[] cipher_name_buf = Encoding.ASCII.GetBytes(real_cipher_name); Array.Resize(ref cipher_name_buf, cipher_name_buf.Length + 1); IntPtr cipher = EVP_get_cipherbyname(cipher_name_buf); if (cipher == IntPtr.Zero) { if (encrypt_func_map != null && encrypt_func_map.ContainsKey(real_cipher_name)) { cipher = encrypt_func_map[real_cipher_name](); } } if (cipher != IntPtr.Zero) { ctx = EVP_CIPHER_CTX_new(); int r = EVP_CipherInit_ex(ctx, cipher, IntPtr.Zero, key, iv, op); if (r == 0) { clean(ctx); return IntPtr.Zero; } } return ctx; } public static int update(IntPtr ctx, byte[] data, int length, byte[] outbuf) { int out_len = 0; EVP_CipherUpdate(ctx, outbuf, ref out_len, data, length); return out_len; } public static void clean(IntPtr ctx) { EVP_CIPHER_CTX_cleanup(ctx); EVP_CIPHER_CTX_free(ctx); } [DllImport("Kernel32.dll")] private static extern IntPtr LoadLibrary(string path); //[DllImport("libdl.so")] //private static extern IntPtr dlopen(String fileName, int flags); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void OpenSSL_add_all_ciphers(); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr EVP_add_cipher(byte[] cipher_name); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr EVP_get_cipherbyname(byte[] cipher_name); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr EVP_aes_128_cfb(); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr EVP_rc4(); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr EVP_CIPHER_CTX_new(); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void EVP_CIPHER_CTX_cleanup(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void EVP_CIPHER_CTX_free(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int EVP_CipherInit_ex(IntPtr ctx, IntPtr cipher, IntPtr _, byte[] key, byte[] iv, int op); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void EVP_CipherUpdate(IntPtr ctx, byte[] output, ref int output_size, byte[] data, int len); } } ================================================ FILE: shadowsocks-csharp/Encryption/LibcryptoEncryptor.cs ================================================ using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace Shadowsocks.Encryption { public class LibcryptoEncryptor : IVEncryptor, IDisposable { const int CIPHER_AES = 1; const int CIPHER_RC4 = 2; const int CIPHER_CAMELLIA = 3; const int CIPHER_OTHER_CFB = 4; private IntPtr _encryptCtx = IntPtr.Zero; private IntPtr _decryptCtx = IntPtr.Zero; public LibcryptoEncryptor(string method, string password, bool cache) : base(method, password, cache) { InitKey(method, password); } public static void InitAviable() { List remove_ciphers = new List(); foreach (string cipher in _ciphers.Keys) { if (!Libcrypto.is_cipher(cipher)) { remove_ciphers.Add(cipher); } } foreach (string cipher in remove_ciphers) { _ciphers.Remove(cipher); } } private static Dictionary _ciphers = new Dictionary { {"aes-128-cbc", new EncryptorInfo(16, 16, false, CIPHER_AES)}, {"aes-192-cbc", new EncryptorInfo(24, 16, false, CIPHER_AES)}, {"aes-256-cbc", new EncryptorInfo(32, 16, false, CIPHER_AES)}, {"aes-128-ctr", new EncryptorInfo(16, 16, true, CIPHER_AES)}, {"aes-192-ctr", new EncryptorInfo(24, 16, true, CIPHER_AES)}, {"aes-256-ctr", new EncryptorInfo(32, 16, true, CIPHER_AES)}, {"aes-128-cfb", new EncryptorInfo(16, 16, true, CIPHER_AES)}, {"aes-192-cfb", new EncryptorInfo(24, 16, true, CIPHER_AES)}, {"aes-256-cfb", new EncryptorInfo(32, 16, true, CIPHER_AES)}, {"aes-128-cfb8", new EncryptorInfo(16, 16, true, CIPHER_AES)}, {"aes-192-cfb8", new EncryptorInfo(24, 16, true, CIPHER_AES)}, {"aes-256-cfb8", new EncryptorInfo(32, 16, true, CIPHER_AES)}, {"aes-128-cfb1", new EncryptorInfo(16, 16, false, CIPHER_AES)}, {"aes-192-cfb1", new EncryptorInfo(24, 16, false, CIPHER_AES)}, {"aes-256-cfb1", new EncryptorInfo(32, 16, false, CIPHER_AES)}, {"camellia-128-cfb", new EncryptorInfo(16, 16, false, CIPHER_CAMELLIA)}, {"camellia-192-cfb", new EncryptorInfo(24, 16, false, CIPHER_CAMELLIA)}, {"camellia-256-cfb", new EncryptorInfo(32, 16, false, CIPHER_CAMELLIA)}, {"bf-cfb", new EncryptorInfo(16, 8, false, CIPHER_OTHER_CFB)}, {"cast5-cfb", new EncryptorInfo(16, 8, false, CIPHER_OTHER_CFB)}, //{"des-cfb", new EncryptorInfo(8, 8, true, CIPHER_OTHER_CFB)}, // weak //{"des-ede3-cfb", new EncryptorInfo(24, 8, true, CIPHER_OTHER_CFB)}, {"idea-cfb", new EncryptorInfo(16, 8, false, CIPHER_OTHER_CFB)}, {"rc2-cfb", new EncryptorInfo(16, 8, false, CIPHER_OTHER_CFB)}, {"rc4", new EncryptorInfo(16, 0, true, CIPHER_RC4)}, // weak {"rc4-md5", new EncryptorInfo(16, 16, true, CIPHER_RC4)}, // weak {"rc4-md5-6", new EncryptorInfo(16, 6, true, CIPHER_RC4)}, // weak {"seed-cfb", new EncryptorInfo(16, 16, false, CIPHER_OTHER_CFB)}, }; public static List SupportedCiphers() { return new List(_ciphers.Keys); } public static bool isSupport() { return Libcrypto.isSupport(); } protected override Dictionary getCiphers() { return _ciphers; } protected override void initCipher(byte[] iv, bool isCipher) { base.initCipher(iv, isCipher); IntPtr ctx; byte[] realkey; if (_method.StartsWith("rc4-md5")) { byte[] temp = new byte[keyLen + ivLen]; realkey = new byte[keyLen]; Array.Copy(_key, 0, temp, 0, keyLen); Array.Copy(iv, 0, temp, keyLen, ivLen); realkey = MbedTLS.MD5(temp); } else { realkey = _key; } if (isCipher) { if (_encryptCtx == IntPtr.Zero) { ctx = Libcrypto.init(Method, realkey, iv, 1); _encryptCtx = ctx; } else { ctx = _encryptCtx; } } else { if (_decryptCtx == IntPtr.Zero) { ctx = Libcrypto.init(Method, realkey, iv, 0); _decryptCtx = ctx; } else { ctx = _decryptCtx; } } } protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) { if (_disposed) { throw new ObjectDisposedException(this.ToString()); } int len = Libcrypto.update(isCipher ? _encryptCtx : _decryptCtx, buf, length, outbuf); } #region IDisposable private bool _disposed; public override void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~LibcryptoEncryptor() { Dispose(false); } protected virtual void Dispose(bool disposing) { lock (this) { if (_disposed) { return; } _disposed = true; } if (disposing) { if (_encryptCtx != IntPtr.Zero) Libcrypto.clean(_encryptCtx); if (_decryptCtx != IntPtr.Zero) Libcrypto.clean(_decryptCtx); _encryptCtx = IntPtr.Zero; _decryptCtx = IntPtr.Zero; } } #endregion } } ================================================ FILE: shadowsocks-csharp/Encryption/MbedTLS.cs ================================================ using System; using System.IO; using System.Runtime.InteropServices; using Shadowsocks.Controller; using Shadowsocks.Properties; using Shadowsocks.Util; namespace Shadowsocks.Encryption { public class MbedTLS { const string DLLNAME = "libsscrypto"; public const int MBEDTLS_ENCRYPT = 1; public const int MBEDTLS_DECRYPT = 0; public const int MD5_CTX_SIZE = 88; public const int MBEDTLS_MD_MD5 = 3; public const int MBEDTLS_MD_SHA1 = 4; public const int MBEDTLS_MD_SHA224 = 5; public const int MBEDTLS_MD_SHA256 = 6; public const int MBEDTLS_MD_SHA384 = 7; public const int MBEDTLS_MD_SHA512 = 8; public const int MBEDTLS_MD_RIPEMD160 = 9; public interface HMAC { byte[] ComputeHash(byte[] buffer, int offset, int count); } public class HMAC_MD5 : HMAC { byte[] key; public HMAC_MD5(byte[] key) { this.key = key; } public byte[] ComputeHash(byte[] buffer, int offset, int count) { byte[] output = new byte[64]; ss_hmac_ex(MBEDTLS_MD_MD5, key, key.Length, buffer, offset, count, output); return output; } } public class HMAC_SHA1 : HMAC { byte[] key; public HMAC_SHA1(byte[] key) { this.key = key; } public byte[] ComputeHash(byte[] buffer, int offset, int count) { byte[] output = new byte[64]; ss_hmac_ex(MBEDTLS_MD_SHA1, key, key.Length, buffer,offset, count, output); return output; } } static MbedTLS() { string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string runningPath = Path.Combine(new Uri(path).LocalPath, @"temp"); // Path.GetTempPath(); if (!Directory.Exists(runningPath)) { Directory.CreateDirectory(runningPath); } string dllPath = Path.Combine(runningPath, "libsscrypto.dll"); try { if (IntPtr.Size == 4) { FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll); } else { FileManager.UncompressFile(dllPath, Resources.libsscrypto64_dll); } } catch (IOException) { } catch (Exception e) { Logging.LogUsefulException(e); } IntPtr module = LoadLibrary(dllPath); } public static byte[] MD5(byte[] input) { byte[] output = new byte[16]; md5(input, input.Length, output); return output; } public static byte[] SHA1(byte[] input) { byte[] output = new byte[20]; ss_md(MBEDTLS_MD_SHA1, input, 0, input.Length, output); return output; } public static byte[] SHA512(byte[] input) { byte[] output = new byte[64]; ss_md(MBEDTLS_MD_SHA512, input, 0, input.Length, output); return output; } [DllImport("Kernel32.dll")] private static extern IntPtr LoadLibrary(string path); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr cipher_info_from_string(string cipher_name); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void cipher_init(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info); // XXX: Check operation before using it [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_reset(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void cipher_free(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void md5(byte[] input, int ilen, byte[] output); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void ss_md(int md_type, byte[] input, int offset, int ilen, byte[] output); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern void ss_hmac_ex(int md_type, byte[] key, int keylen, byte[] input, int offset, int ilen, byte[] output); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public static extern int cipher_get_size_ex(); } } ================================================ FILE: shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs ================================================ using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace Shadowsocks.Encryption { public class MbedTLSEncryptor : IVEncryptor, IDisposable { const int CIPHER_RC4 = 1; const int CIPHER_AES = 2; const int CIPHER_BLOWFISH = 3; const int CIPHER_CAMELLIA = 4; private IntPtr _encryptCtx = IntPtr.Zero; private IntPtr _decryptCtx = IntPtr.Zero; public MbedTLSEncryptor(string method, string password, bool cache) : base(method, password, cache) { } private static Dictionary _ciphers = new Dictionary { { "aes-128-cbc", new EncryptorInfo(16, 16, false, CIPHER_AES, "AES-128-CBC") }, { "aes-192-cbc", new EncryptorInfo(24, 16, false, CIPHER_AES, "AES-192-CBC") }, { "aes-256-cbc", new EncryptorInfo(32, 16, false, CIPHER_AES, "AES-256-CBC") }, { "aes-128-ctr", new EncryptorInfo(16, 16, true, CIPHER_AES, "AES-128-CTR") }, { "aes-192-ctr", new EncryptorInfo(24, 16, true, CIPHER_AES, "AES-192-CTR") }, { "aes-256-ctr", new EncryptorInfo(32, 16, true, CIPHER_AES, "AES-256-CTR") }, { "aes-128-cfb", new EncryptorInfo(16, 16, true, CIPHER_AES, "AES-128-CFB128") }, { "aes-192-cfb", new EncryptorInfo(24, 16, true, CIPHER_AES, "AES-192-CFB128") }, { "aes-256-cfb", new EncryptorInfo(32, 16, true, CIPHER_AES, "AES-256-CFB128") }, { "bf-cfb", new EncryptorInfo(16, 8, false, CIPHER_BLOWFISH, "BLOWFISH-CFB64") }, { "camellia-128-cfb", new EncryptorInfo(16, 16, false, CIPHER_CAMELLIA, "CAMELLIA-128-CFB128") }, { "camellia-192-cfb", new EncryptorInfo(24, 16, false, CIPHER_CAMELLIA, "CAMELLIA-192-CFB128") }, { "camellia-256-cfb", new EncryptorInfo(32, 16, false, CIPHER_CAMELLIA, "CAMELLIA-256-CFB128") }, { "rc4", new EncryptorInfo(16, 0, true, CIPHER_RC4, "ARC4-128") }, { "rc4-md5", new EncryptorInfo(16, 16, true, CIPHER_RC4, "ARC4-128") }, { "rc4-md5-6", new EncryptorInfo(16, 6, true, CIPHER_RC4, "ARC4-128") }, }; public static List SupportedCiphers() { return new List(_ciphers.Keys); } protected override Dictionary getCiphers() { return _ciphers; } protected override void initCipher(byte[] iv, bool isCipher) { base.initCipher(iv, isCipher); IntPtr ctx = Marshal.AllocHGlobal(MbedTLS.cipher_get_size_ex()); if (isCipher) { _encryptCtx = ctx; } else { _decryptCtx = ctx; } byte[] realkey; if (_method.StartsWith("rc4-")) { byte[] temp = new byte[keyLen + ivLen]; realkey = new byte[keyLen]; Array.Copy(_key, 0, temp, 0, keyLen); Array.Copy(iv, 0, temp, keyLen, ivLen); realkey = MbedTLS.MD5(temp); } else { realkey = _key; } MbedTLS.cipher_init(ctx); if (MbedTLS.cipher_setup( ctx, MbedTLS.cipher_info_from_string( getInfo().name ) ) != 0 ) throw new Exception("Cannot initialize mbed TLS cipher context"); /* * MbedTLS takes key length by bit * cipher_setkey() will set the correct key schedule * and operation * * MBEDTLS_AES_{EN,DE}CRYPT * == MBEDTLS_BLOWFISH_{EN,DE}CRYPT * == MBEDTLS_CAMELLIA_{EN,DE}CRYPT * == MBEDTLS_{EN,DE}CRYPT * */ if (MbedTLS.cipher_setkey(ctx, realkey, keyLen * 8, isCipher ? MbedTLS.MBEDTLS_ENCRYPT : MbedTLS.MBEDTLS_DECRYPT) != 0 ) throw new Exception("Cannot set mbed TLS cipher key"); if (MbedTLS.cipher_set_iv(ctx, iv, ivLen) != 0) throw new Exception("Cannot set mbed TLS cipher IV"); if (MbedTLS.cipher_reset(ctx) != 0) throw new Exception("Cannot finalize mbed TLS cipher context"); } protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) { // C# could be multi-threaded if (_disposed) { throw new ObjectDisposedException(this.ToString()); } if (MbedTLS.cipher_update(isCipher ? _encryptCtx : _decryptCtx, buf, length, outbuf, ref length) != 0 ) throw new Exception("Cannot update mbed TLS cipher context"); } #region IDisposable private bool _disposed; public override void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~MbedTLSEncryptor() { Dispose(false); } protected virtual void Dispose(bool disposing) { lock (this) { if (_disposed) { return; } _disposed = true; } if (disposing) { if (_encryptCtx != IntPtr.Zero) { MbedTLS.cipher_free(_encryptCtx); Marshal.FreeHGlobal(_encryptCtx); _encryptCtx = IntPtr.Zero; } if (_decryptCtx != IntPtr.Zero) { MbedTLS.cipher_free(_decryptCtx); Marshal.FreeHGlobal(_decryptCtx); _decryptCtx = IntPtr.Zero; } } } #endregion } } ================================================ FILE: shadowsocks-csharp/Encryption/RSA.cs ================================================ using System; using System.Text; using System.Security.Cryptography; using System.IO; namespace Shadowsocks.Encryption { class RSA { public static bool SignatureVerify(string p_strKeyPublic, byte[] rgb, byte[] rgbSignature) { try { RSACryptoServiceProvider key = new RSACryptoServiceProvider(); key.FromXmlString(p_strKeyPublic); RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key); deformatter.SetHashAlgorithm("SHA512"); if (deformatter.VerifySignature(rgb, rgbSignature)) { return true; } return false; } catch { return false; } } } } ================================================ FILE: shadowsocks-csharp/Encryption/Sodium.cs ================================================ using Shadowsocks.Controller; using Shadowsocks.Properties; using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace Shadowsocks.Encryption { public class Sodium { const string DLLNAME = "libsscrypto"; static Sodium() { string dllPath = Path.Combine(Path.Combine(System.Windows.Forms.Application.StartupPath, @"temp"), "libsscrypto.dll"); try { if (IntPtr.Size == 4) { FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll); } else { FileManager.UncompressFile(dllPath, Resources.libsscrypto64_dll); } LoadLibrary(dllPath); } catch (IOException) { } catch (Exception e) { Console.WriteLine(e.ToString()); } } [DllImport("Kernel32.dll")] private static extern IntPtr LoadLibrary(string path); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static void crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static void crypto_stream_xsalsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static void crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static void crypto_stream_xchacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic, byte[] k); } } ================================================ FILE: shadowsocks-csharp/Encryption/SodiumEncryptor.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Encryption { public class SodiumEncryptor : IVEncryptor, IDisposable { const int CIPHER_SALSA20 = 1; const int CIPHER_CHACHA20 = 2; const int CIPHER_CHACHA20_IETF = 3; const int CIPHER_XSALSA20 = 4 + 1; const int CIPHER_XCHACHA20 = 4 + 2; const int SODIUM_BLOCK_SIZE = 64; protected int _encryptBytesRemaining; protected int _decryptBytesRemaining; protected ulong _encryptIC; protected ulong _decryptIC; protected byte[] _encryptBuf; protected byte[] _decryptBuf; private delegate void crypto_stream(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); private crypto_stream encryptor_delegate; public SodiumEncryptor(string method, string password, bool cache) : base(method, password, cache) { InitKey(method, password); _encryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; _decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; switch (_cipher) { case CIPHER_SALSA20: encryptor_delegate = Sodium.crypto_stream_salsa20_xor_ic; break; case CIPHER_CHACHA20: encryptor_delegate = Sodium.crypto_stream_chacha20_xor_ic; break; case CIPHER_XSALSA20: encryptor_delegate = Sodium.crypto_stream_xsalsa20_xor_ic; break; case CIPHER_XCHACHA20: encryptor_delegate = Sodium.crypto_stream_xchacha20_xor_ic; break; case CIPHER_CHACHA20_IETF: encryptor_delegate = crypto_stream_chacha20_ietf_xor_ic; break; } } private static Dictionary _ciphers = new Dictionary { {"salsa20", new EncryptorInfo(32, 8, true, CIPHER_SALSA20)}, {"chacha20", new EncryptorInfo(32, 8, true, CIPHER_CHACHA20)}, {"xsalsa20", new EncryptorInfo(32, 24, true, CIPHER_XSALSA20)}, {"xchacha20", new EncryptorInfo(32, 24, true, CIPHER_XCHACHA20)}, {"chacha20-ietf", new EncryptorInfo(32, 12, true, CIPHER_CHACHA20_IETF)}, }; protected override Dictionary getCiphers() { return _ciphers; } public static List SupportedCiphers() { return new List(_ciphers.Keys); } protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) { 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); encryptor_delegate(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key); 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; } } public override void ResetEncrypt() { _encryptIVSent = false; _encryptIC = 0; _encryptBytesRemaining = 0; } public override void ResetDecrypt() { _decryptIVReceived = 0; _decryptIC = 0; _decryptBytesRemaining = 0; } void crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k) { Sodium.crypto_stream_chacha20_ietf_xor_ic(c, m, mlen, n, (uint)ic, k); } public override void Dispose() { } } } ================================================ FILE: shadowsocks-csharp/Model/Configuration.cs ================================================ using Shadowsocks.Controller; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using System.Threading; using Shadowsocks.Encryption; namespace Shadowsocks.Model { public class UriVisitTime : IComparable { public DateTime visitTime; public string uri; public int index; public int CompareTo(object other) { if (!(other is UriVisitTime)) throw new InvalidOperationException("CompareTo: Not a UriVisitTime"); if (Equals(other)) return 0; return visitTime.CompareTo(((UriVisitTime)other).visitTime); } } public enum PortMapType : int { Forward = 0, ForceProxy, RuleProxy } public enum ProxyRuleMode : int { Disable = 0, BypassLan, BypassLanAndChina, BypassLanAndNotChina, UserCustom = 16, } [Serializable] public class PortMapConfig { public bool enable; public PortMapType type; public string id; public string server_addr; public int server_port; public string remarks; } public class PortMapConfigCache { public PortMapType type; public string id; public Server server; public string server_addr; public int server_port; } [Serializable] public class ServerSubscribe { private static string DEFAULT_FEED_URL = "https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/free/freenodeplain.txt"; //private static string OLD_DEFAULT_FEED_URL = "https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/free/freenode.txt"; public string URL = DEFAULT_FEED_URL; public string Group; public UInt64 LastUpdateTime; } public class GlobalConfiguration { public static string config_password = ""; } [Serializable()] class ConfigurationException : System.Exception { public ConfigurationException() : base() { } public ConfigurationException(string message) : base(message) { } public ConfigurationException(string message, System.Exception inner) : base(message, inner) { } protected ConfigurationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } } [Serializable()] class ConfigurationWarning : System.Exception { public ConfigurationWarning() : base() { } public ConfigurationWarning(string message) : base(message) { } public ConfigurationWarning(string message, System.Exception inner) : base(message, inner) { } protected ConfigurationWarning(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } } [Serializable] public class Configuration { public List configs; public int index; public bool random; public int sysProxyMode; public bool shareOverLan; public int localPort; public string localAuthPassword; public string localDnsServer; public string dnsServer; public int reconnectTimes; public string balanceAlgorithm; public bool randomInGroup; public int TTL; public int connectTimeout; public int proxyRuleMode; public bool proxyEnable; public bool pacDirectGoProxy; public int proxyType; public string proxyHost; public int proxyPort; public string proxyAuthUser; public string proxyAuthPass; public string proxyUserAgent; public string authUser; public string authPass; public bool autoBan; public bool sameHostForSameTarget; public int keepVisitTime; public bool isHideTips; public bool nodeFeedAutoUpdate; public List serverSubscribes; public Dictionary token = new Dictionary(); public Dictionary portMap = new Dictionary(); private Dictionary serverStrategyMap = new Dictionary(); private Dictionary portMapCache = new Dictionary(); private LRUCache uricache = new LRUCache(180); private static string CONFIG_FILE = "gui-config.json"; public static void SetPassword(string password) { GlobalConfiguration.config_password = password; } public static bool SetPasswordTry(string old_password, string password) { if (old_password != GlobalConfiguration.config_password) return false; return true; } public bool KeepCurrentServer(int localPort, string targetAddr, string id) { if (sameHostForSameTarget && targetAddr != null) { lock (serverStrategyMap) { if (!serverStrategyMap.ContainsKey(localPort)) serverStrategyMap[localPort] = new ServerSelectStrategy(); ServerSelectStrategy serverStrategy = serverStrategyMap[localPort]; if (uricache.ContainsKey(targetAddr)) { UriVisitTime visit = uricache.Get(targetAddr); int index = -1; for (int i = 0; i < configs.Count; ++i) { if (configs[i].id == id) { index = i; break; } } if (index >= 0 && visit.index == index && configs[index].enable) { uricache.Del(targetAddr); return true; } } } } return false; } public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc filter, string targetAddr = null, bool cfgRandom = false, bool usingRandom = false, bool forceRandom = false) { lock (serverStrategyMap) { if (!serverStrategyMap.ContainsKey(localPort)) serverStrategyMap[localPort] = new ServerSelectStrategy(); ServerSelectStrategy serverStrategy = serverStrategyMap[localPort]; uricache.SetTimeout(keepVisitTime); uricache.Sweep(); if (sameHostForSameTarget && !forceRandom && targetAddr != null && uricache.ContainsKey(targetAddr)) { UriVisitTime visit = uricache.Get(targetAddr); if (visit.index < configs.Count && configs[visit.index].enable && configs[visit.index].ServerSpeedLog().ErrorContinurousTimes == 0) { uricache.Del(targetAddr); return configs[visit.index]; } } if (forceRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, delegate (Server server, Server selServer) { if (selServer != null) return selServer.group == server.group; return false; }, true); } else { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, filter, true); } if (index == -1) return GetErrorServer(); return configs[index]; } else if (usingRandom && cfgRandom) { int index; if (filter == null && randomInGroup) { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, delegate (Server server, Server selServer) { if (selServer != null) return selServer.group == server.group; return false; }); } else { index = serverStrategy.Select(configs, this.index, balanceAlgorithm, filter); } if (index == -1) return GetErrorServer(); if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = index; visit.visitTime = DateTime.Now; uricache.Set(targetAddr, visit); } return configs[index]; } else { if (index >= 0 && index < configs.Count) { int selIndex = index; if (usingRandom) { for (int i = 0; i < configs.Count; ++i) { if (configs[selIndex].isEnable()) { break; } else { selIndex = (selIndex + 1) % configs.Count; } } } if (targetAddr != null) { UriVisitTime visit = new UriVisitTime(); visit.uri = targetAddr; visit.index = selIndex; visit.visitTime = DateTime.Now; uricache.Set(targetAddr, visit); } return configs[selIndex]; } else { return GetErrorServer(); } } } } public void FlushPortMapCache() { portMapCache = new Dictionary(); Dictionary id2server = new Dictionary(); Dictionary server_group = new Dictionary(); foreach (Server s in configs) { id2server[s.id] = s; if (!string.IsNullOrEmpty(s.group)) { server_group[s.group] = 1; } } foreach (KeyValuePair pair in portMap) { int key = 0; PortMapConfig pm = pair.Value; if (!pm.enable) continue; if (id2server.ContainsKey(pm.id) || server_group.ContainsKey(pm.id) || pm.id == null || pm.id.Length == 0) { } else continue; try { key = int.Parse(pair.Key); } catch (FormatException) { continue; } portMapCache[key] = new PortMapConfigCache { type = pm.type, id = pm.id, server = id2server.ContainsKey(pm.id) ? id2server[pm.id] : null, server_addr = pm.server_addr, server_port = pm.server_port }; } lock (serverStrategyMap) { List remove_ports = new List(); foreach (KeyValuePair pair in serverStrategyMap) { if (portMapCache.ContainsKey(pair.Key)) continue; remove_ports.Add(pair.Key); } foreach (int port in remove_ports) { serverStrategyMap.Remove(port); } if (!portMapCache.ContainsKey(localPort)) serverStrategyMap.Remove(localPort); } uricache.Clear(); } public Dictionary GetPortMapCache() { return portMapCache; } public static void CheckServer(Server server) { CheckPort(server.server_port); if (server.server_udp_port != 0) CheckPort(server.server_udp_port); try { CheckPassword(server.password); } catch (ConfigurationWarning cw) { server.password = ""; MessageBox.Show(cw.Message, cw.Message, MessageBoxButtons.OK, MessageBoxIcon.Warning); } CheckServer(server.server); } public Configuration() { index = 0; localPort = 1080; reconnectTimes = 2; keepVisitTime = 180; connectTimeout = 5; dnsServer = ""; localDnsServer = ""; balanceAlgorithm = "LowException"; random = false; sysProxyMode = (int)ProxyMode.Global; proxyRuleMode = (int)ProxyRuleMode.BypassLanAndChina; nodeFeedAutoUpdate = true; serverSubscribes = new List() { }; configs = new List() { GetDefaultServer() }; } public void CopyFrom(Configuration config) { configs = config.configs; index = config.index; random = config.random; sysProxyMode = config.sysProxyMode; shareOverLan = config.shareOverLan; localPort = config.localPort; reconnectTimes = config.reconnectTimes; balanceAlgorithm = config.balanceAlgorithm; randomInGroup = config.randomInGroup; TTL = config.TTL; connectTimeout = config.connectTimeout; dnsServer = config.dnsServer; localDnsServer = config.localDnsServer; proxyEnable = config.proxyEnable; pacDirectGoProxy = config.pacDirectGoProxy; proxyType = config.proxyType; proxyHost = config.proxyHost; proxyPort = config.proxyPort; proxyAuthUser = config.proxyAuthUser; proxyAuthPass = config.proxyAuthPass; proxyUserAgent = config.proxyUserAgent; authUser = config.authUser; authPass = config.authPass; autoBan = config.autoBan; sameHostForSameTarget = config.sameHostForSameTarget; keepVisitTime = config.keepVisitTime; isHideTips = config.isHideTips; nodeFeedAutoUpdate = config.nodeFeedAutoUpdate; serverSubscribes = config.serverSubscribes; } public void FixConfiguration() { if (localPort == 0) { localPort = 1080; } if (keepVisitTime == 0) { keepVisitTime = 180; } if (portMap == null) { portMap = new Dictionary(); } if (token == null) { token = new Dictionary(); } if (connectTimeout == 0) { connectTimeout = 10; reconnectTimes = 2; TTL = 180; keepVisitTime = 180; } if (localAuthPassword == null || localAuthPassword.Length < 16) { localAuthPassword = randString(20); } Dictionary id = new Dictionary(); if (index < 0 || index >= configs.Count) index = 0; foreach (Server server in configs) { if (id.ContainsKey(server.id)) { byte[] new_id = new byte[16]; Util.Utils.RandBytes(new_id, new_id.Length); server.id = BitConverter.ToString(new_id).Replace("-", ""); } else { id[server.id] = 0; } } } private static string randString(int len) { string set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; string ret = ""; Random random = new Random(); for (int i = 0; i < len; ++i) { ret += set[random.Next(set.Length)]; } return ret; } public static Configuration LoadFile(string filename) { try { string configContent = File.ReadAllText(filename); return Load(configContent); } catch (Exception e) { if (!(e is FileNotFoundException)) { Console.WriteLine(e); } return new Configuration(); } } public static Configuration Load() { return LoadFile(CONFIG_FILE); } public static void Save(Configuration config) { if (config.index >= config.configs.Count) { config.index = config.configs.Count - 1; } if (config.index < 0) { config.index = 0; } try { string jsonString = SimpleJson.SimpleJson.SerializeObject(config); if (GlobalConfiguration.config_password.Length > 0) { IEncryptor encryptor = EncryptorFactory.GetEncryptor("aes-256-cfb", GlobalConfiguration.config_password, false); byte[] cfg_data = UTF8Encoding.UTF8.GetBytes(jsonString); byte[] cfg_encrypt = new byte[cfg_data.Length + 128]; int data_len = 0; const int buffer_size = 32768; byte[] input = new byte[buffer_size]; byte[] ouput = new byte[buffer_size + 128]; for (int start_pos = 0; start_pos < cfg_data.Length; start_pos += buffer_size) { int len = Math.Min(cfg_data.Length - start_pos, buffer_size); int out_len; Buffer.BlockCopy(cfg_data, start_pos, input, 0, len); encryptor.Encrypt(input, len, ouput, out out_len); Buffer.BlockCopy(ouput, 0, cfg_encrypt, data_len, out_len); data_len += out_len; } jsonString = System.Convert.ToBase64String(cfg_encrypt, 0, data_len); } using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create))) { sw.Write(jsonString); sw.Flush(); } } catch (IOException e) { Console.Error.WriteLine(e); } } public static Configuration Load(string config_str) { try { if (GlobalConfiguration.config_password.Length > 0) { byte[] cfg_encrypt = System.Convert.FromBase64String(config_str); IEncryptor encryptor = EncryptorFactory.GetEncryptor("aes-256-cfb", GlobalConfiguration.config_password, false); byte[] cfg_data = new byte[cfg_encrypt.Length]; int data_len = 0; const int buffer_size = 32768; byte[] input = new byte[buffer_size]; byte[] ouput = new byte[buffer_size + 128]; for (int start_pos = 0; start_pos < cfg_encrypt.Length; start_pos += buffer_size) { int len = Math.Min(cfg_encrypt.Length - start_pos, buffer_size); int out_len; Buffer.BlockCopy(cfg_encrypt, start_pos, input, 0, len); encryptor.Decrypt(input, len, ouput, out out_len); Buffer.BlockCopy(ouput, 0, cfg_data, data_len, out_len); data_len += out_len; } config_str = UTF8Encoding.UTF8.GetString(cfg_data, 0, data_len); } } catch { } try { Configuration config = SimpleJson.SimpleJson.DeserializeObject(config_str, new JsonSerializerStrategy()); config.FixConfiguration(); return config; } catch { } return null; } public static Server GetDefaultServer() { return new Server(); } public bool isDefaultConfig() { if (configs.Count == 1 && configs[0].server == Configuration.GetDefaultServer().server) return true; return false; } public static Server CopyServer(Server server) { Server s = new Server(); s.server = server.server; s.server_port = server.server_port; s.method = server.method; s.protocol = server.protocol; s.protocolparam = server.protocolparam ?? ""; s.obfs = server.obfs; s.obfsparam = server.obfsparam ?? ""; s.password = server.password; s.remarks = server.remarks; s.group = server.group; s.udp_over_tcp = server.udp_over_tcp; s.server_udp_port = server.server_udp_port; return s; } public static Server GetErrorServer() { Server server = new Server(); server.server = "invalid"; return server; } public static void CheckPort(int port) { if (port <= 0 || port > 65535) { throw new ConfigurationException(I18N.GetString("Port out of range")); } } private static void CheckPassword(string password) { if (string.IsNullOrEmpty(password)) { throw new ConfigurationWarning(I18N.GetString("Password are blank")); //throw new ConfigurationException(I18N.GetString("Password can not be blank")); } } private static void CheckServer(string server) { if (string.IsNullOrEmpty(server)) { throw new ConfigurationException(I18N.GetString("Server IP can not be blank")); } } private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy { // convert string to int public override object DeserializeObject(object value, Type type) { if (type == typeof(Int32) && value.GetType() == typeof(string)) { return Int32.Parse(value.ToString()); } return base.DeserializeObject(value, type); } } } [Serializable] public class ServerTrans { public long totalUploadBytes; public long totalDownloadBytes; void AddUpload(long bytes) { //lock (this) { totalUploadBytes += bytes; } } void AddDownload(long bytes) { //lock (this) { totalDownloadBytes += bytes; } } } [Serializable] public class ServerTransferTotal { private static string LOG_FILE = "transfer_log.json"; public Dictionary servers = new Dictionary(); private int saveCounter; private DateTime saveTime; public static ServerTransferTotal Load() { try { string config_str = File.ReadAllText(LOG_FILE); ServerTransferTotal config = new ServerTransferTotal(); try { if (GlobalConfiguration.config_password.Length > 0) { byte[] cfg_encrypt = System.Convert.FromBase64String(config_str); IEncryptor encryptor = EncryptorFactory.GetEncryptor("aes-256-cfb", GlobalConfiguration.config_password, false); byte[] cfg_data = new byte[cfg_encrypt.Length]; int data_len; encryptor.Decrypt(cfg_encrypt, cfg_encrypt.Length, cfg_data, out data_len); config_str = UTF8Encoding.UTF8.GetString(cfg_data, 0, data_len); } } catch { } config.servers = SimpleJson.SimpleJson.DeserializeObject>(config_str, new JsonSerializerStrategy()); config.Init(); return config; } catch (Exception e) { if (!(e is FileNotFoundException)) { Console.WriteLine(e); } return new ServerTransferTotal(); } } public void Init() { saveCounter = 256; saveTime = DateTime.Now; if (servers == null) servers = new Dictionary(); } public static void Save(ServerTransferTotal config) { try { using (StreamWriter sw = new StreamWriter(File.Open(LOG_FILE, FileMode.Create))) { string jsonString = SimpleJson.SimpleJson.SerializeObject(config.servers); if (GlobalConfiguration.config_password.Length > 0) { IEncryptor encryptor = EncryptorFactory.GetEncryptor("aes-256-cfb", GlobalConfiguration.config_password, false); byte[] cfg_data = UTF8Encoding.UTF8.GetBytes(jsonString); byte[] cfg_encrypt = new byte[cfg_data.Length + 128]; int data_len; encryptor.Encrypt(cfg_data, cfg_data.Length, cfg_encrypt, out data_len); jsonString = System.Convert.ToBase64String(cfg_encrypt, 0, data_len); } sw.Write(jsonString); sw.Flush(); } } catch (IOException e) { Console.Error.WriteLine(e); } } public void Clear(string server) { lock (servers) { if (servers.ContainsKey(server)) { ((ServerTrans)servers[server]).totalUploadBytes = 0; ((ServerTrans)servers[server]).totalDownloadBytes = 0; } } } public void AddUpload(string server, Int64 size) { lock (servers) { if (!servers.ContainsKey(server)) servers.Add(server, new ServerTrans()); ((ServerTrans)servers[server]).totalUploadBytes += size; } if (--saveCounter <= 0) { saveCounter = 256; if ((DateTime.Now - saveTime).TotalMinutes > 10) { lock (servers) { Save(this); saveTime = DateTime.Now; } } } } public void AddDownload(string server, Int64 size) { lock (servers) { if (!servers.ContainsKey(server)) servers.Add(server, new ServerTrans()); ((ServerTrans)servers[server]).totalDownloadBytes += size; } if (--saveCounter <= 0) { saveCounter = 256; if ((DateTime.Now - saveTime).TotalMinutes > 10) { lock (servers) { Save(this); saveTime = DateTime.Now; } } } } private class JsonSerializerStrategy : SimpleJson.PocoJsonSerializerStrategy { public override object DeserializeObject(object value, Type type) { if (type == typeof(Int64) && value.GetType() == typeof(string)) { return Int64.Parse(value.ToString()); } else if (type == typeof(object)) { return base.DeserializeObject(value, typeof(ServerTrans)); } return base.DeserializeObject(value, type); } } } } ================================================ FILE: shadowsocks-csharp/Model/Host.cs ================================================ using System; using System.Collections.Generic; using System.Net; using System.Text; namespace Shadowsocks.Model { class HostNode { public bool include_sub; public string addr; public Dictionary subnode; public HostNode() { include_sub = false; addr = ""; subnode = new Dictionary(); } public HostNode(bool sub, string addr) { include_sub = sub; this.addr = addr; subnode = null; } } public class HostMap { Dictionary root = new Dictionary(); IPSegment ips = new IPSegment("remoteproxy"); static HostMap instance = new HostMap(); const string HOST_FILENAME = "user.rule"; public static HostMap Instance() { return instance; } public void Clear(HostMap newInstance) { if (newInstance == null) { instance = new HostMap(); } else { instance = newInstance; } } public void AddHost(string host, string addr) { IPAddress ip_addr = null; if (IPAddress.TryParse(host, out ip_addr)) { string[] addr_parts = addr.Split(new char[] { ' ', '\t', }, 2, StringSplitOptions.RemoveEmptyEntries); if (addr_parts.Length >= 2) { ips.insert(new IPAddressCmp(host), new IPAddressCmp(addr_parts[0]), addr_parts[1]); return; } } string[] parts = host.Split('.'); Dictionary node = root; bool include_sub = false; int end = 0; if (parts[0].Length == 0) { end = 1; include_sub = true; } for (int i = parts.Length - 1; i > end; --i) { if (!node.ContainsKey(parts[i])) { node[parts[i]] = new HostNode(); } if (node[parts[i]].subnode == null) { node[parts[i]].subnode = new Dictionary(); } node = node[parts[i]].subnode; } node[parts[end]] = new HostNode(include_sub, addr); } public bool GetHost(string host, out string addr) { string[] parts = host.Split('.'); Dictionary node = root; addr = null; for (int i = parts.Length - 1; i >= 0; --i) { if (!node.ContainsKey(parts[i])) { return false; } if (node[parts[i]].addr.Length > 0 || node[parts[i]].include_sub) { addr = node[parts[i]].addr; return true; } if (node.ContainsKey("*")) { addr = node["*"].addr; return true; } if (node[parts[i]].subnode == null) { return false; } node = node[parts[i]].subnode; } return false; } public bool GetIP(IPAddress ip, out string addr) { string host = ip.ToString(); addr = ips.Get(new IPAddressCmp(host)) as string; return addr != null; } public bool LoadHostFile() { string filename = HOST_FILENAME; string absFilePath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, filename); if (System.IO.File.Exists(absFilePath)) { try { using (System.IO.StreamReader stream = System.IO.File.OpenText(absFilePath)) { while (true) { string line = stream.ReadLine(); if (line == null) break; if (line.Length > 0 && line.StartsWith("#")) continue; string[] parts = line.Split(new char[] { ' ', '\t', }, 2, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) continue; AddHost(parts[0], parts[1]); } } return true; } catch { return false; } } return false; } } } ================================================ FILE: shadowsocks-csharp/Model/IPRangeSet.cs ================================================ using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; namespace Shadowsocks.Model { public class IPRangeSet { private const string APNIC_FILENAME = "delegated-apnic-latest"; private const string APNIC_EXT_FILENAME = "delegated-apnic-extended-latest"; private const string CHN_FILENAME = "chn_ip.txt"; private uint[] _set; public IPRangeSet() { _set = new uint[256 * 256 * 8]; } public void InsertRange(uint begin, uint end) { begin /= 256; end /= 256; for (uint i = begin; i <= end; ++i) { uint pos = i / 32; int mv = (int)(i & 31); _set[pos] |= (1u << mv); } } public void Insert(uint begin, uint size) { InsertRange(begin, begin + size - 1); } public void Insert(IPAddress addr, uint size) { byte[] bytes_addr = addr.GetAddressBytes(); Array.Reverse(bytes_addr); Insert(BitConverter.ToUInt32(bytes_addr, 0), size); } public void Insert(IPAddress addr_beg, IPAddress addr_end) { byte[] bytes_addr_beg = addr_beg.GetAddressBytes(); Array.Reverse(bytes_addr_beg); byte[] bytes_addr_end = addr_end.GetAddressBytes(); Array.Reverse(bytes_addr_end); InsertRange(BitConverter.ToUInt32(bytes_addr_beg, 0), BitConverter.ToUInt32(bytes_addr_end, 0)); } public bool isIn(uint ip) { ip /= 256; uint pos = ip / 32; int mv = (int)(ip & 31); return (_set[pos] & (1u << mv)) != 0; } public bool IsInIPRange(IPAddress addr) { byte[] bytes_addr = addr.GetAddressBytes(); Array.Reverse(bytes_addr); return isIn(BitConverter.ToUInt32(bytes_addr, 0)); } public bool LoadApnic(string zone) { string filename = APNIC_EXT_FILENAME; string absFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, filename); if (!File.Exists(absFilePath)) { filename = APNIC_FILENAME; absFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, filename); } if (File.Exists(absFilePath)) { try { using (StreamReader stream = File.OpenText(absFilePath)) { using (StreamWriter out_stream = new StreamWriter(File.OpenWrite(CHN_FILENAME))) { while (true) { string line = stream.ReadLine(); if (line == null) break; string[] parts = line.Split('|'); if (parts.Length < 7) continue; if (parts[0] != "apnic" || parts[1] != zone || parts[2] != "ipv4") continue; IPAddress addr; IPAddress.TryParse(parts[3], out addr); uint size = UInt32.Parse(parts[4]); Insert(addr, size); byte[] addr_bytes = addr.GetAddressBytes(); Array.Reverse(addr_bytes); uint ip_addr = BitConverter.ToUInt32(addr_bytes, 0); ip_addr += size - 1; addr_bytes = BitConverter.GetBytes(ip_addr); Array.Reverse(addr_bytes); out_stream.Write(parts[3] + " " + (new IPAddress(addr_bytes)).ToString() + "\r\n"); } } } return true; } catch { return false; } } return false; } public bool LoadChn() { string absFilePath = Path.Combine(System.Windows.Forms.Application.StartupPath, CHN_FILENAME); if (File.Exists(absFilePath)) { try { using (StreamReader stream = File.OpenText(absFilePath)) { while (true) { string line = stream.ReadLine(); if (line == null) break; string[] parts = line.Split(' '); if (parts.Length < 2) continue; IPAddress addr_beg, addr_end; IPAddress.TryParse(parts[0], out addr_beg); IPAddress.TryParse(parts[1], out addr_end); Insert(addr_beg, addr_end); } } } catch { return false; } } else { return !LoadApnic("CN"); } return false; } public void Reverse() { IPAddress addr_beg, addr_end; IPAddress.TryParse("240.0.0.0", out addr_beg); IPAddress.TryParse("255.255.255.255", out addr_end); Insert(addr_beg, addr_end); for (uint i = 0; i < _set.Length; ++i) { _set[i] = ~_set[i]; } } } } ================================================ FILE: shadowsocks-csharp/Model/IPSegment.cs ================================================ using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Model { public class IPAddressCmp : System.Net.IPAddress, IComparable { public IPAddressCmp(System.Net.IPAddress ip) : base(ip.GetAddressBytes()) { } public IPAddressCmp(byte[] ip) : base(ip) { } public IPAddressCmp(string ip) : base(IPAddressCmp.FromString(ip).GetAddressBytes()) { } public static System.Net.IPAddress FromString(string ip) { System.Net.IPAddress addr = null; TryParse(ip, out addr); return addr; } public int CompareTo(object obj) { byte[] b1 = GetAddressBytes(); byte[] b2 = (obj as IPAddressCmp).GetAddressBytes(); int len = Math.Min(b1.Length, b2.Length); for (int i = 0; i < b1.Length; ++i) { if (b1[i] < b2[i]) return -1; else if (b1[i] > b2[i]) return 1; } if (b1.Length < b2.Length) return -1; else if (b1.Length > b2.Length) return 1; return 0; } public IPAddressCmp ToIPv6() { if (AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) return this; byte[] b1 = GetAddressBytes(); byte[] br = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0}; b1.CopyTo(br, 12); return new IPAddressCmp(br); } public IPAddressCmp Inc() { byte[] b = GetAddressBytes(); int i = b.Length - 1; for (; i >= 0; --i) { if (b[i] == 0xff) { b[i] = 0; } else { b[i]++; break; } } if (i < 0) { return new IPAddressCmp(GetAddressBytes()); } return new IPAddressCmp(b); } } public class IPSegment { protected SortedList list = new SortedList(); public IPSegment(object val = null) { list.Add(new IPAddressCmp(new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }), val); } public bool insert(IPAddressCmp ipStart, IPAddressCmp ipEnd, object val) { IPAddressCmp s = ipStart.ToIPv6(); IPAddressCmp e = ipEnd.ToIPv6().Inc(); object ed_val = null; if (list.Contains(s)) { ed_val = list[s]; list[s] = val; } else { list[s] = val; int index = list.IndexOfKey(s) - 1; if (index >= 0) { ed_val = list.GetByIndex(index); } } { int index = list.IndexOfKey(s); while (index > 0) { if (val.Equals(list.GetByIndex(index - 1))) { list.RemoveAt(index); --index; } else break; } ++index; bool keep = false; while(index < list.Count) { int cmp = (list.GetKey(index) as IPAddressCmp).CompareTo(e); if (cmp >= 0) { if (cmp == 0) keep = true; break; } ed_val = list.GetByIndex(index); list.RemoveAt(index); } if (!keep) { list[e] = ed_val; index = list.IndexOfKey(e); while (index > 0) { if (ed_val.Equals(list.GetByIndex(index - 1))) { list.RemoveAt(index); --index; } else break; } while (index + 1 < list.Count) { if (ed_val.Equals(list.GetByIndex(index + 1))) { list.RemoveAt(index); } else break; } } } return true; } public object Get(IPAddressCmp ip) { IPAddressCmp ip_addr = ip.ToIPv6(); int l = 0, r = list.Count - 1; while (l < r) { int m = (l + r + 1) / 2; IPAddressCmp v = list.GetKey(m) as IPAddressCmp; int cmp = v.CompareTo(ip_addr); if (cmp > 0) { r = m - 1; } else if (cmp < 0) { l = m; } else if (cmp == 0) { return list[m]; } } return list.GetByIndex(l); } } } ================================================ FILE: shadowsocks-csharp/Model/LRUCache.cs ================================================ using System; using System.Collections.Generic; using System.Text; using Shadowsocks.Controller; namespace Shadowsocks.Model { public class LRUCache { protected Dictionary _store = new Dictionary(); protected Dictionary _key_2_time = new Dictionary(); protected Dictionary _time_2_key = new Dictionary(); protected object _lock = new object(); protected int _sweep_time; public LRUCache(int sweep_time = 60 * 60) { _sweep_time = sweep_time; } public void SetTimeout(int time) { _sweep_time = time; } public void Clear() { lock (_lock) { _store.Clear(); _key_2_time.Clear(); _time_2_key.Clear(); } } public bool isTimeout(K key) { lock (_lock) { if ((DateTime.Now - _key_2_time[key]).TotalSeconds > _sweep_time) { return true; } return false; } } public bool ContainsKey(K key) { lock (_lock) { return _store.ContainsKey(key); } } public V Get(K key) { lock (_lock) { if (_store.ContainsKey(key)) { DateTime t = _key_2_time[key]; _key_2_time.Remove(key); _time_2_key.Remove(t); t = DateTime.Now; while (_time_2_key.ContainsKey(t)) { t = t.AddTicks(1); } _time_2_key[t] = key; _key_2_time[key] = t; return _store[key]; } return default(V); } } public V Set(K key, V val) { lock (_lock) { DateTime t; if (_store.ContainsKey(key)) { t = _key_2_time[key]; _key_2_time.Remove(key); _time_2_key.Remove(t); } t = DateTime.Now; while (_time_2_key.ContainsKey(t)) { t = t.AddTicks(1); } _time_2_key[t] = key; _key_2_time[key] = t; _store[key] = val; return val; } } public void Del(K key) { lock (_lock) { DateTime t; if (_store.ContainsKey(key)) { t = _key_2_time[key]; _key_2_time.Remove(key); _time_2_key.Remove(t); _store.Remove(key); } } } public void Sweep() { lock (_lock) { DateTime now = DateTime.Now; int sweep = 0; for (int i = 0; i < 100; ++i) { bool finish = false; foreach (KeyValuePair p in _time_2_key) { if ((now - p.Key).TotalSeconds < _sweep_time) { finish = true; break; } _key_2_time.Remove(p.Value); _time_2_key.Remove(p.Key); _store.Remove(p.Value); Logging.Debug("sweep [" + p.Key.ToString() + "]: " + p.Value.ToString()); sweep += 1; break; } if (finish) break; } if (sweep > 0) { Logging.Debug("sweep " + sweep.ToString() + " items"); } } } } } ================================================ FILE: shadowsocks-csharp/Model/MinSearchTree.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Model { public struct MinSearchTreeNode { public int range_min; public int range_max; public int count; public long min; } public class MinSearchTree { protected int _count; protected int _size; protected int _level; protected MinSearchTreeNode[] _tree; public MinSearchTree(int size) { _level = GetLevel(size); _size = size; _count = size + (1 << _level); _tree = new MinSearchTreeNode[2 << _level]; } public int Size { get { return _size; } } protected int GetLevel(int size) { int ret = 0; for (int s = size; s > 1; s >>=1) { ret++; } if (size != (1 << ret)) ++ret; return ret; } protected void _Init(int index, int level, int range_min, int range_max) { _tree[index].range_min = range_min; _tree[index].range_max = range_max; _tree[index].min = 0; _tree[index].count = range_max - range_min; if (level >= 0) { int l = index * 2; int r = l + 1; _Init(l, level - 1, range_min, range_min + (1 << level)); _Init(r, level - 1, range_min + (1 << level), range_max); } } public void Init() { _Init(1, _level - 1, 0, _size); for (int i = _count; i < (2 << _level); ++i) { _tree[i].min = Int64.MaxValue; } int offset = 1 << _level; for (int i = _count >> 1; i < offset; ++i) { Maintain(i); } } public MinSearchTree Clone() { MinSearchTree tree = new MinSearchTree(_size); for (int i = 0; i < (2 << _level); ++i) { tree._tree[i] = _tree[i]; } return tree; } public void Update(int[] add_list) { int offset = 1 << _level; for (int i = 0; i < add_list.Length; ++i) { if (add_list[i] > 0) { _tree[offset + i].min += add_list[i]; add_list[i] = 0; Maintain((offset + i) >> 1); } } } public void Update(Dictionary add_map) { int offset = 1 << _level; foreach (KeyValuePair pair in add_map) { _tree[offset + pair.Key].min += pair.Value; Maintain((offset + pair.Key) >> 1); } add_map.Clear(); if (_tree[1].min > int.MaxValue) { for (int i = 1; i < _tree.Length; ++i) { _tree[i].min -= int.MaxValue; } } } protected void Maintain(int index) { for (; index > 0; index >>= 1) { int l = index * 2; int r = l + 1; long min = Math.Min(_tree[l].min, _tree[r].min); int count = 0; if (min == _tree[l].min) count += _tree[l].count; if (min == _tree[r].min) count += _tree[r].count; if (_tree[index].min == min && _tree[index].count == count) return; _tree[index].min = min; _tree[index].count = count; } } public void Update(int index, int add = 1) { index = (1 << _level) + index; _tree[index].min += add; Maintain(index >> 1); } public int FindMinCount(int index, int range_min, int range_max, out long min_val) { if (range_min == _tree[index].range_min && range_max == _tree[index].range_max) { min_val = _tree[index].min; return _tree[index].count; } int l = index * 2; int r = l + 1; int count = 0; long sub_min_val = Int64.MaxValue; if (_tree[l].range_max > range_min) { long out_val; int cnt = FindMinCount(l, range_min, Math.Min(range_max, _tree[l].range_max), out out_val); if (out_val < sub_min_val) { sub_min_val = out_val; count = cnt; } else if (out_val == sub_min_val) { count += cnt; } } if (_tree[r].range_min < range_max) { long out_val; int cnt = FindMinCount(r, Math.Max(range_min, _tree[r].range_min), range_max, out out_val); if (out_val < sub_min_val) { sub_min_val = out_val; count = cnt; } else if (out_val == sub_min_val) { count += cnt; } } min_val = sub_min_val; return count; } public int FindNthMin(int index, int range_min, int range_max, int nth, long val) { if (_tree[index].range_min + 1 == _tree[index].range_max) { return index - (1 << _level); } int l = index * 2; int r = l + 1; if (_tree[l].range_max > range_min) { if (_tree[r].range_min < range_max) { long out_val; int cnt = FindMinCount(l, range_min, _tree[l].range_max, out out_val); if (out_val != val) cnt = 0; if (cnt > nth) { return FindNthMin(l, range_min, _tree[l].range_max, nth, val); } else { return FindNthMin(r, _tree[r].range_min, range_max, nth - cnt, val); } } else { return FindNthMin(l, Math.Max(range_min, _tree[l].range_min), range_max, nth, val); } } else { return FindNthMin(r, range_min, Math.Min(range_max, _tree[r].range_max), nth, val); } } public int FindMinCount2(int index, int range_min, int range_max, out long min_val) { int offset = 1 << _level; long min = Int64.MaxValue; int cnt = 0; for (int i = range_min; i < range_max; ++i) { if (_tree[offset + i].min < min) { min = _tree[offset + i].min; } } for (int i = range_min; i < range_max; ++i) { if (_tree[offset + i].min == min) { ++cnt; } } min_val = min; return cnt; } public int FindNthMin2(int range_min, int range_max, int nth) { int offset = 1 << _level; long min = Int64.MaxValue; int cnt = 0; for (int i = range_min; i < range_max; ++i) { if (_tree[offset + i].min < min) { min = _tree[offset + i].min; } } for (int i = range_min; i < range_max; ++i) { if (_tree[offset + i].min == min) { if (cnt == nth) return i; ++cnt; } } return -1; } public int RandomFindIndex(int range_min, int range_max, Random random) { long out_val; int count = FindMinCount(1, range_min, range_max, out out_val); int nth = random.Next(count); int index = FindNthMin(1, range_min, range_max, nth, out_val); return index; } public long GetMin(int range_min, int range_max) { long ret; int cnt = FindMinCount(1, range_min, range_max, out ret); return ret; } } } ================================================ FILE: shadowsocks-csharp/Model/Server.cs ================================================ using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; #if !_CONSOLE using SimpleJson; #endif using Shadowsocks.Controller; using System.Text.RegularExpressions; using System.Net; using System.Net.Sockets; using System.Linq; namespace Shadowsocks.Model { public class DnsBuffer { public IPAddress ip; public DateTime updateTime; public string host; public bool force_expired; public bool isExpired(string host) { if (updateTime == null) return true; if (this.host != host) return true; if (force_expired && (DateTime.Now - updateTime).TotalMinutes > 1) return true; return (DateTime.Now - updateTime).TotalMinutes > 30; } public void UpdateDns(string host, IPAddress ip) { updateTime = DateTime.Now; this.ip = new IPAddress(ip.GetAddressBytes()); this.host = host; force_expired = false; } } public class Connections { private System.Collections.Generic.Dictionary sockets = new Dictionary(); public bool AddRef(IHandler socket) { lock (this) { if (sockets.ContainsKey(socket)) { sockets[socket] += 1; } else { sockets[socket] = 1; } return true; } } public bool DecRef(IHandler socket) { lock (this) { if (sockets.ContainsKey(socket)) { sockets[socket] -= 1; if (sockets[socket] == 0) { sockets.Remove(socket); } } else { return false; } return true; } } public void CloseAll() { IHandler[] s; lock (this) { s = new IHandler[sockets.Count]; sockets.Keys.CopyTo(s, 0); } foreach (IHandler handler in s) { try { handler.Shutdown(); } catch { } } } public int Count { get { return sockets.Count; } } } [Serializable] public class Server { public string id; public string server; public int server_port; public int server_udp_port; public string password; public string method; public string protocol; public string protocolparam; public string obfs; public string obfsparam; public string remarks_base64; public string group; public bool enable; public bool udp_over_tcp; public int latency; public static int LATENCY_ERROR = -2; public static int LATENCY_PENDING = -1; public static int LATENCY_TESTING = 0; private object protocoldata; private object obfsdata; private ServerSpeedLog serverSpeedLog = new ServerSpeedLog(); private DnsBuffer dnsBuffer = new DnsBuffer(); private Connections Connections = new Connections(); private static Server forwardServer = new Server(); public void CopyServer(Server Server) { protocoldata = Server.protocoldata; obfsdata = Server.obfsdata; serverSpeedLog = Server.serverSpeedLog; dnsBuffer = Server.dnsBuffer; Connections = Server.Connections; enable = Server.enable; } public void CopyServerInfo(Server Server) { remarks = Server.remarks; group = Server.group; } public static Server GetForwardServerRef() { return forwardServer; } public void SetConnections(Connections Connections) { this.Connections = Connections; } public Connections GetConnections() { return Connections; } public DnsBuffer DnsBuffer() { return dnsBuffer; } public ServerSpeedLog ServerSpeedLog() { return serverSpeedLog; } public void SetServerSpeedLog(ServerSpeedLog log) { serverSpeedLog = log; } public string remarks { get { if (remarks_base64.Length == 0) { return string.Empty; } try { return Util.Base64.DecodeUrlSafeBase64(remarks_base64); } catch (FormatException) { var old = remarks_base64; remarks = remarks_base64; return old; } } set { remarks_base64 = Util.Base64.EncodeUrlSafeBase64(value); } } public string FriendlyName() { if (string.IsNullOrEmpty(server)) { return I18N.GetString("New server"); } if (string.IsNullOrEmpty(remarks_base64)) { if (server.IndexOf(':') >= 0) { return "[" + server + "]:" + server_port; } else { return server + ":" + server_port; } } else { if (server.IndexOf(':') >= 0) { return remarks + " ([" + server + "]:" + server_port + ")"; } else { return remarks + " (" + server + ":" + server_port + ")"; } } } public string HiddenName(bool hide = true) { if (string.IsNullOrEmpty(server)) { return I18N.GetString("New server"); } string server_alter_name = server; if (hide) { server_alter_name = Util.ServerName.HideServerAddr(server); } if (string.IsNullOrEmpty(remarks_base64)) { if (server.IndexOf(':') >= 0) { return "[" + server_alter_name + "]:" + server_port; } else { return server_alter_name + ":" + server_port; } } else { if (server.IndexOf(':') >= 0) { return remarks + " ([" + server_alter_name + "]:" + server_port + ")"; } else { return remarks + " (" + server_alter_name + ":" + server_port + ")"; } } } public Server Clone() { Server ret = new Server(); ret.server = server; ret.server_port = server_port; ret.password = password; ret.method = method; ret.protocol = protocol; ret.obfs = obfs; ret.obfsparam = obfsparam ?? ""; ret.remarks_base64 = remarks_base64; ret.group = group; ret.enable = enable; ret.udp_over_tcp = udp_over_tcp; ret.id = id; ret.protocoldata = protocoldata; ret.obfsdata = obfsdata; return ret; } public Server() { server = "server host"; server_port = 8388; method = "aes-256-cfb"; protocol = "origin"; protocolparam = ""; obfs = "plain"; obfsparam = ""; password = "0"; remarks_base64 = ""; group = "FreeSSR-public"; udp_over_tcp = false; enable = true; latency = LATENCY_PENDING; byte[] id = new byte[16]; Util.Utils.RandBytes(id, id.Length); this.id = BitConverter.ToString(id).Replace("-", ""); } public Server(string ssURL, string force_group) : this() { if (ssURL.StartsWith("ss://", StringComparison.OrdinalIgnoreCase)) { ServerFromSS(ssURL, force_group); } else if (ssURL.StartsWith("ssr://", StringComparison.OrdinalIgnoreCase)) { ServerFromSSR(ssURL, force_group); } else { throw new FormatException(); } } public bool isMatchServer(Server server) { if (this.server == server.server && server_port == server.server_port && server_udp_port == server.server_udp_port && method == server.method && protocol == server.protocol && protocolparam == server.protocolparam && obfs == server.obfs && obfsparam == server.obfsparam && password == server.password && udp_over_tcp == server.udp_over_tcp ) return true; return false; } private Dictionary ParseParam(string param_str) { Dictionary params_dict = new Dictionary(); string[] obfs_params = param_str.Split('&'); foreach (string p in obfs_params) { if (p.IndexOf('=') > 0) { int index = p.IndexOf('='); string key, val; key = p.Substring(0, index); val = p.Substring(index + 1); params_dict[key] = val; } } return params_dict; } public void ServerFromSSR(string ssrURL, string force_group) { // ssr://host:port:protocol:method:obfs:base64pass/?obfsparam=base64&remarks=base64&group=base64&udpport=0&uot=1 Match ssr = Regex.Match(ssrURL, "ssr://([A-Za-z0-9_-]+)", RegexOptions.IgnoreCase); if (!ssr.Success) throw new FormatException(); string data = Util.Base64.DecodeUrlSafeBase64(ssr.Groups[1].Value); Dictionary params_dict = new Dictionary(); Match match = null; int param_start_pos = data.IndexOf("?"); if (param_start_pos > 0) { params_dict = ParseParam(data.Substring(param_start_pos + 1)); data = data.Substring(0, param_start_pos); } if (data.IndexOf("/") >= 0) { data = data.Substring(0, data.LastIndexOf("/")); } Regex UrlFinder = new Regex("^(.+):([^:]+):([^:]*):([^:]+):([^:]*):([^:]+)"); match = UrlFinder.Match(data); if (match == null || !match.Success) throw new FormatException(); server = match.Groups[1].Value; server_port = int.Parse(match.Groups[2].Value); protocol = match.Groups[3].Value.Length == 0 ? "origin" : match.Groups[3].Value; protocol = protocol.Replace("_compatible", ""); method = match.Groups[4].Value; obfs = match.Groups[5].Value.Length == 0 ? "plain" : match.Groups[5].Value; obfs = obfs.Replace("_compatible", ""); password = Util.Base64.DecodeStandardSSRUrlSafeBase64(match.Groups[6].Value); if (params_dict.ContainsKey("protoparam")) { protocolparam = Util.Base64.DecodeStandardSSRUrlSafeBase64(params_dict["protoparam"]); } if (params_dict.ContainsKey("obfsparam")) { obfsparam = Util.Base64.DecodeStandardSSRUrlSafeBase64(params_dict["obfsparam"]); } if (params_dict.ContainsKey("remarks")) { remarks = Util.Base64.DecodeStandardSSRUrlSafeBase64(params_dict["remarks"]); } if (params_dict.ContainsKey("group")) { group = Util.Base64.DecodeStandardSSRUrlSafeBase64(params_dict["group"]); } else group = ""; if (params_dict.ContainsKey("uot")) { udp_over_tcp = int.Parse(params_dict["uot"]) != 0; } if (params_dict.ContainsKey("udpport")) { server_udp_port = int.Parse(params_dict["udpport"]); } if (!String.IsNullOrEmpty(force_group)) group = force_group; } public void ServerFromSS(string ssURL, string force_group) { Regex UrlFinder = new Regex("^(?i)ss://([A-Za-z0-9+-/=_]+)(#(.+))?", RegexOptions.IgnoreCase), DetailsParser = new Regex("^((?.+):(?.*)@(?.+?)" + ":(?\\d+?))$", RegexOptions.IgnoreCase); var match = UrlFinder.Match(ssURL); if (!match.Success) throw new FormatException(); var base64 = match.Groups[1].Value; match = DetailsParser.Match(Encoding.UTF8.GetString(Convert.FromBase64String( base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '=')))); protocol = "origin"; method = match.Groups["method"].Value; password = match.Groups["password"].Value; server = match.Groups["hostname"].Value; server_port = int.Parse(match.Groups["port"].Value); if (!String.IsNullOrEmpty(force_group)) group = force_group; else group = ""; } public string GetSSLinkForServer() { string parts = method + ":" + password + "@" + server + ":" + server_port; string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts)).Replace("=", ""); return "ss://" + base64; } public string GetSSRLinkForServer() { string main_part = server + ":" + server_port + ":" + protocol + ":" + method + ":" + obfs + ":" + Util.Base64.EncodeUrlSafeBase64(password); string param_str = "obfsparam=" + Util.Base64.EncodeUrlSafeBase64(obfsparam ?? ""); if (!string.IsNullOrEmpty(protocolparam)) { param_str += "&protoparam=" + Util.Base64.EncodeUrlSafeBase64(protocolparam); } if (!string.IsNullOrEmpty(remarks)) { param_str += "&remarks=" + Util.Base64.EncodeUrlSafeBase64(remarks); } if (!string.IsNullOrEmpty(group)) { param_str += "&group=" + Util.Base64.EncodeUrlSafeBase64(group); } if (udp_over_tcp) { param_str += "&uot=" + "1"; } if (server_udp_port > 0) { param_str += "&udpport=" + server_udp_port.ToString(); } string base64 = Util.Base64.EncodeUrlSafeBase64(main_part + "/?" + param_str); return "ssr://" + base64; } public bool isEnable() { return enable; } public void setEnable(bool enable) { this.enable = enable; } public object getObfsData() { return this.obfsdata; } public void setObfsData(object data) { this.obfsdata = data; } public object getProtocolData() { return this.protocoldata; } public void setProtocolData(object data) { this.protocoldata = data; } public void tcpingLatency() { var latencies = new List(); var sock = new TcpClient(); var stopwatch = new Stopwatch(); stopwatch.Start(); try { Dns.GetHostAddresses(server); } catch (Exception) { latency = LATENCY_ERROR; return; } var result = sock.BeginConnect(server, server_port, null, null); if (result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(2))) { stopwatch.Stop(); latencies.Add(stopwatch.Elapsed.TotalMilliseconds); } else { stopwatch.Stop(); } try { sock.Close(); } catch (Exception) { } if (latencies.Count != 0) { latency = (int)latencies.Average(); } else { latency = LATENCY_ERROR; } } } } ================================================ FILE: shadowsocks-csharp/Model/ServerSelectStrategy.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Model { public class ServerSelectStrategy { public delegate bool FilterFunc(Server server, Server selServer); // return true if select the server private Random randomGennarator; private int lastSelectIndex; private string lastSelectID; private DateTime lastSelectTime; private int lastUserSelectIndex; private const int MAX_CHANCE = 10000; private const int ERROR_PENALTY = MAX_CHANCE / 20; private const int CONNECTION_PENALTY = MAX_CHANCE / 100; private const int MIN_CHANCE = 10; private struct ServerIndex { public int index; public Server server; public ServerIndex(int i, Server s) { index = i; this.server = s; } }; private int lowerBound(List data, double target) { int left = 0; int right = data.Count - 1; while (left < right) { int mid = (left + right) / 2; if (data[mid] >= target) right = mid; else if (data[mid] < target) left = mid + 1; } return left; } private double Algorithm2(ServerSpeedLog serverSpeedLog) // perfer less delay { if (serverSpeedLog.ErrorContinurousTimes >= 20) return 1; else if (serverSpeedLog.ErrorContinurousTimes >= 10) return MIN_CHANCE; else if (serverSpeedLog.AvgConnectTime < 0 && serverSpeedLog.TotalConnectTimes >= 3) return MIN_CHANCE; else if (serverSpeedLog.TotalConnectTimes < 1) return MAX_CHANCE; else { long avgConnectTime = serverSpeedLog.AvgConnectTime <= 0 ? 1 : serverSpeedLog.AvgConnectTime; if (serverSpeedLog.TotalConnectTimes >= 1 && serverSpeedLog.AvgConnectTime < 0) avgConnectTime = 5000; long connections = serverSpeedLog.TotalConnectTimes - serverSpeedLog.TotalDisconnectTimes; double chance = MAX_CHANCE * 10.0 / avgConnectTime - connections * CONNECTION_PENALTY; if (chance > MAX_CHANCE) chance = MAX_CHANCE; chance -= serverSpeedLog.ErrorContinurousTimes * ERROR_PENALTY; if (chance < MIN_CHANCE) chance = MIN_CHANCE; return chance; } } private double Algorithm3(ServerSpeedLog serverSpeedLog) // perfer less error { if (serverSpeedLog.ErrorContinurousTimes >= 20) return 1; else if (serverSpeedLog.ErrorContinurousTimes >= 10) return MIN_CHANCE; else if (serverSpeedLog.AvgConnectTime < 0 && serverSpeedLog.TotalConnectTimes >= 3) return MIN_CHANCE; else if (serverSpeedLog.TotalConnectTimes < 1) return MAX_CHANCE; else { long avgConnectTime = serverSpeedLog.AvgConnectTime <= 0 ? 1 : serverSpeedLog.AvgConnectTime / 1000 * 1000; if (serverSpeedLog.TotalConnectTimes >= 1 && serverSpeedLog.AvgConnectTime < 0) avgConnectTime = 5000; long connections = serverSpeedLog.TotalConnectTimes - serverSpeedLog.TotalDisconnectTimes; double chance = MAX_CHANCE * 1.0 / (avgConnectTime / 500 + 1) - connections * CONNECTION_PENALTY; if (chance > MAX_CHANCE) chance = MAX_CHANCE; chance -= serverSpeedLog.ErrorContinurousTimes * ERROR_PENALTY; if (chance < MIN_CHANCE) chance = MIN_CHANCE; return chance; } } private double Algorithm4(ServerSpeedLog serverSpeedLog, long avg_speed, double zero_chance) // perfer fast speed { if (serverSpeedLog.ErrorContinurousTimes >= 20) return 1; else if (serverSpeedLog.ErrorContinurousTimes >= 10) return MIN_CHANCE; else if (serverSpeedLog.AvgConnectTime < 0 && serverSpeedLog.TotalConnectTimes >= 3) return MIN_CHANCE; else if (serverSpeedLog.TotalConnectTimes < 1) return MAX_CHANCE; else { long avgConnectTime = serverSpeedLog.AvgConnectTime <= 0 ? 1 : serverSpeedLog.AvgConnectTime / 2000 * 2000; long speed_u, speed_d; serverSpeedLog.GetTransSpeed(out speed_u, out speed_d); if (serverSpeedLog.TotalConnectTimes >= 1 && serverSpeedLog.AvgConnectTime < 0) avgConnectTime = 5000; double speed_mul = speed_d > avg_speed ? 1.0 : speed_d == 0 ? zero_chance : speed_d < avg_speed / 2 ? 0.001 : 0.005; long connections = serverSpeedLog.TotalConnectTimes - serverSpeedLog.TotalDisconnectTimes; double chance = MAX_CHANCE * speed_mul / (avgConnectTime / 500 + 1) - connections * CONNECTION_PENALTY; if (chance > MAX_CHANCE) chance = MAX_CHANCE; chance -= serverSpeedLog.ErrorContinurousTimes * ERROR_PENALTY; if (chance < MIN_CHANCE) chance = MIN_CHANCE; return chance; } } protected int SubSelect(List configs, int curIndex, string algorithm, FilterFunc filter, bool forceChange) { if (randomGennarator == null) { randomGennarator = new Random(); lastSelectIndex = -1; } if (configs.Count <= lastSelectIndex || lastSelectIndex < 0) { lastSelectIndex = -1; lastSelectTime = DateTime.Now; lastUserSelectIndex = -1; } else { if (configs[lastSelectIndex].id != lastSelectID) { if (lastSelectID != null) { for (int i = 0; i < configs.Count; ++i) { if (configs[i].id == lastSelectID) { lastSelectIndex = i; break; } } } if (configs[lastSelectIndex].id != lastSelectID) { lastSelectIndex = -1; lastSelectTime = DateTime.Now; lastUserSelectIndex = -1; } } } if (lastUserSelectIndex != curIndex) { if (configs.Count > curIndex && curIndex >= 0 && algorithm != "Timer") { lastSelectIndex = curIndex; } lastUserSelectIndex = curIndex; } if (lastSelectIndex == -1) { if (configs.Count > curIndex && curIndex >= 0) { lastSelectIndex = curIndex; } } if (configs.Count > 0) { List serverList = new List(); for (int i = 0; i < configs.Count; ++i) { if (configs[i].isEnable()) { if (filter != null) { if (!filter(configs[i], lastSelectIndex < 0 ? null : configs[lastSelectIndex])) continue; } serverList.Add(new ServerIndex(i, configs[i])); } } if (serverList.Count == 0 && filter != null) { for (int i = 0; i < configs.Count; ++i) { if (!filter(configs[i], lastSelectIndex < 0 ? null : configs[lastSelectIndex])) continue; serverList.Add(new ServerIndex(i, configs[i])); } } if (forceChange && serverList.Count > 1 && algorithm != "OneByOne") { for (int i = 0; i < serverList.Count; ++i) { if (serverList[i].index == lastSelectIndex) { serverList.RemoveAt(i); break; } } } if (serverList.Count == 0) { int i = lastSelectIndex; if (i >= 0 && i < configs.Count && configs[i].isEnable()) serverList.Add(new ServerIndex(i, configs[i])); } int serverListIndex = -1; if (serverList.Count > 0) { if (algorithm == "OneByOne") { int selIndex = -1; for (int i = 0; i < serverList.Count; ++i) { if (serverList[i].index == lastSelectIndex) { selIndex = i; break; } } serverListIndex = serverList[(selIndex + 1) % serverList.Count].index; } else if (algorithm == "Random") { serverListIndex = randomGennarator.Next(serverList.Count); serverListIndex = serverList[serverListIndex].index; } else if (algorithm == "LowException" || algorithm == "Timer" || algorithm == "FastDownloadSpeed") { if (algorithm == "Timer") { if ((DateTime.Now - lastSelectTime).TotalSeconds > 60 * 5) { lastSelectTime = DateTime.Now; } else { if (configs.Count > lastSelectIndex && lastSelectIndex >= 0 && configs[lastSelectIndex].isEnable() && !forceChange) { return lastSelectIndex; } } } List chances = new List(); double lastBeginVal = 0; if (algorithm == "FastDownloadSpeed") { long avg_speed = 1024 * 64; long sum_speed = 0; int sum_cnt = 0; int zero_cnt = 0; foreach (ServerIndex s in serverList) { long speed_u, speed_d; s.server.ServerSpeedLog().GetTransSpeed(out speed_u, out speed_d); if (speed_d == 0) ++zero_cnt; else { sum_speed += speed_d; ++sum_cnt; } } double zero_chance = 0.5; if (sum_cnt > 0) { avg_speed = sum_speed / sum_cnt; if (zero_cnt + sum_cnt > 0) zero_chance = 0.1 * sum_cnt / (zero_cnt + sum_cnt); } foreach (ServerIndex s in serverList) { double chance = Algorithm4(s.server.ServerSpeedLog(), avg_speed, zero_chance); if (chance > 0) { chances.Add(lastBeginVal + chance); lastBeginVal += chance; } } } else { foreach (ServerIndex s in serverList) { double chance = Algorithm3(s.server.ServerSpeedLog()); if (chance > 0) { chances.Add(lastBeginVal + chance); lastBeginVal += chance; } } } { double target = randomGennarator.NextDouble() * lastBeginVal; serverListIndex = lowerBound(chances, target); serverListIndex = serverList[serverListIndex].index; return serverListIndex; } } else //if (algorithm == (int)SelectAlgorithm.LowLatency || algorithm == (int)SelectAlgorithm.SelectedFirst) { List chances = new List(); double lastBeginVal = 0; foreach (ServerIndex s in serverList) { double chance = Algorithm2(s.server.ServerSpeedLog()); if (chance > 0) { chances.Add(lastBeginVal + chance); lastBeginVal += chance; } } if (algorithm == "SelectedFirst" && randomGennarator.Next(3) == 0 && configs[curIndex].isEnable()) { for (int i = 0; i < serverList.Count; ++i) { if (curIndex == serverList[i].index) { return curIndex; } } } { double target = randomGennarator.NextDouble() * lastBeginVal; serverListIndex = lowerBound(chances, target); serverListIndex = serverList[serverListIndex].index; return serverListIndex; } } } return serverListIndex; } else { return -1; } } public int Select(List configs, int curIndex, string algorithm, FilterFunc filter, bool forceChange = false) { lastSelectIndex = SubSelect(configs, curIndex, algorithm, filter, forceChange); if (lastSelectIndex >= 0 && lastSelectIndex < configs.Count) { lastSelectID = configs[lastSelectIndex].id; } else { lastSelectID = null; } return lastSelectIndex; } } } ================================================ FILE: shadowsocks-csharp/Model/ServerSpeedLog.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Model { public class TransLog { public int size; public int firstsize; public int times; public DateTime recvTime; public DateTime endTime; public TransLog(int s, DateTime t) { firstsize = s; size = s; recvTime = t; endTime = t; times = 1; } } public class ErrorLog { public int errno; public DateTime time; public ErrorLog(int no) { errno = no; time = DateTime.Now; } } [Serializable] public class ServerSpeedLogShow { public long totalConnectTimes; public long totalDisconnectTimes; public long errorConnectTimes; public long errorTimeoutTimes; public long errorDecodeTimes; public long errorEmptyTimes; public long errorContinurousTimes; public long errorLogTimes; public long totalUploadBytes; public long totalDownloadBytes; public long totalDownloadRawBytes; public long avgConnectTime; public long avgDownloadBytes; public long maxDownloadBytes; public long avgUploadBytes; public long maxUploadBytes; } public class ServerSpeedLog { private long totalConnectTimes = 0; private long totalDisconnectTimes = 0; private long errorConnectTimes = 0; private long errorTimeoutTimes = 0; private long errorDecodeTimes = 0; private long errorEmptyTimes = 0; private long errorContinurousTimes = 0; private long transUpload = 0; private long transDownload = 0; private long transDownloadRaw = 0; private List downTransLog = new List(); private List upTransLog = new List(); private long maxTransDownload = 0; private long maxTransUpload = 0; private int avgConnectTime = -1; //private List speedLog = null; private LinkedList errList = new LinkedList(); private const int avgTime = 5; public ServerSpeedLog() { } public ServerSpeedLog(long upload, long download) { lock (this) { transUpload = upload; transDownload = download; } } public void GetTransSpeed(out long upload, out long download) { upload = maxTransUpload; download = maxTransDownload; } public void ClearTrans() { lock (this) { transUpload = 0; transDownload = 0; } } public ServerSpeedLogShow Translate() { ServerSpeedLogShow ret = new ServerSpeedLogShow(); lock (this) { Sweep(); ret.avgDownloadBytes = AvgDownloadBytes; ret.avgUploadBytes = AvgUploadBytes; ret.avgConnectTime = AvgConnectTime; ret.maxDownloadBytes = maxTransDownload; ret.maxUploadBytes = maxTransUpload; ret.totalConnectTimes = totalConnectTimes; ret.totalDisconnectTimes = totalDisconnectTimes; ret.errorConnectTimes = errorConnectTimes; ret.errorTimeoutTimes = errorTimeoutTimes; ret.errorDecodeTimes = errorDecodeTimes; ret.errorEmptyTimes = errorEmptyTimes; ret.errorLogTimes = errList.Count; ret.errorContinurousTimes = errorContinurousTimes; ret.totalUploadBytes = transUpload; ret.totalDownloadBytes = transDownload; ret.totalDownloadRawBytes = transDownloadRaw; } return ret; } public long TotalConnectTimes { get { lock (this) { return totalConnectTimes; } } } public long TotalDisconnectTimes { get { lock (this) { return totalDisconnectTimes; } } } public long ErrorConnectTimes { get { lock (this) { return errorConnectTimes; } } } public long ErrorTimeoutTimes { get { lock (this) { return errorTimeoutTimes; } } } public long ErrorEncryptTimes { get { lock (this) { return errorDecodeTimes; } } } public long ErrorContinurousTimes { get { lock (this) { return errorContinurousTimes; } } } protected static long UpdateMaxTrans(long lastMaxTrans, List transAvgLog) { if (transAvgLog.Count > 1) { long avg_totalBytes = 0; double avg_totalTime = 0; for (int i = 0; i < transAvgLog.Count; ++i) { avg_totalBytes += transAvgLog[i].size - transAvgLog[i].firstsize; avg_totalTime += (transAvgLog[i].endTime - transAvgLog[i].recvTime).TotalSeconds; } return (long)(avg_totalBytes / avg_totalTime); } return lastMaxTrans; } public long AvgDownloadBytes { get { List transLog; lock (this) { transLog = new List(); for (int i = 0; i < downTransLog.Count; ++i) { transLog.Add(downTransLog[i]); } } { long totalBytes = 0; double totalTime = 0; if (transLog.Count == 0 || transLog.Count > 0 && DateTime.Now > transLog[transLog.Count - 1].recvTime.AddSeconds(avgTime)) { return 0; } for (int i = 0; i < transLog.Count; ++i) { totalBytes += transLog[i].size; } totalBytes -= transLog[0].firstsize; if (transLog.Count > 1) totalTime = (transLog[transLog.Count - 1].endTime - transLog[0].recvTime).TotalSeconds; if (totalTime > 0.2) { long ret = (long)(totalBytes / totalTime); return ret; } else return 0; } } } public long AvgUploadBytes { get { List transLog; lock (this) { if (this.upTransLog == null) return 0; transLog = new List(); for (int i = 0; i < upTransLog.Count; ++i) { transLog.Add(upTransLog[i]); } } { long totalBytes = 0; double totalTime = 0; if (transLog.Count == 0 || transLog.Count > 0 && DateTime.Now > transLog[transLog.Count - 1].recvTime.AddSeconds(avgTime)) { return 0; } for (int i = 0; i < transLog.Count; ++i) { totalBytes += transLog[i].size; } totalBytes -= transLog[0].firstsize; if (transLog.Count > 1) totalTime = (transLog[transLog.Count - 1].endTime - transLog[0].recvTime).TotalSeconds; if (totalTime > 0.2) { long ret = (long)(totalBytes / totalTime); return ret; } else return 0; } } } public long AvgConnectTime { get { return avgConnectTime; } } public void ClearError() { lock (this) { if (totalConnectTimes > totalDisconnectTimes) totalConnectTimes -= totalDisconnectTimes; else totalConnectTimes = 0; totalDisconnectTimes = 0; errorConnectTimes = 0; errorTimeoutTimes = 0; errorDecodeTimes = 0; errorEmptyTimes = 0; errList.Clear(); errorContinurousTimes = 0; } } public void ClearMaxSpeed() { lock (this) { maxTransDownload = 0; maxTransUpload = 0; } } public void Clear() { lock (this) { if (totalConnectTimes > totalDisconnectTimes) totalConnectTimes -= totalDisconnectTimes; else totalConnectTimes = 0; totalDisconnectTimes = 0; errorConnectTimes = 0; errorTimeoutTimes = 0; errorDecodeTimes = 0; errorEmptyTimes = 0; errList.Clear(); errorContinurousTimes = 0; transUpload = 0; transDownload = 0; transDownloadRaw = 0; maxTransDownload = 0; maxTransUpload = 0; } } public void AddConnectTimes() { lock (this) { totalConnectTimes += 1; } } public void AddDisconnectTimes() { lock (this) { totalDisconnectTimes += 1; } } protected void Sweep() { while (errList.Count > 0) { if ((DateTime.Now - errList.First.Value.time).TotalMinutes < 30 && errList.Count < 100) break; int errCode = errList.First.Value.errno; errList.RemoveFirst(); if (errCode == 1) { if (errorConnectTimes > 0) errorConnectTimes -= 1; } else if (errCode == 2) { if (errorTimeoutTimes > 0) errorTimeoutTimes -= 1; } else if (errCode == 3) { if (errorDecodeTimes > 0) errorDecodeTimes -= 1; } else if (errCode == 4) { if (errorEmptyTimes > 0) errorEmptyTimes -= 1; } } } public void AddNoErrorTimes() { lock (this) { errList.AddLast(new ErrorLog(0)); errorEmptyTimes = 0; Sweep(); } } public void AddErrorTimes() { lock (this) { errorConnectTimes += 1; errorContinurousTimes += 2; errList.AddLast(new ErrorLog(1)); Sweep(); } } public void AddTimeoutTimes() { lock (this) { errorTimeoutTimes += 1; errorContinurousTimes += 1; errList.AddLast(new ErrorLog(2)); Sweep(); } } public void AddErrorDecodeTimes() { lock (this) { errorDecodeTimes += 1; errorContinurousTimes += 10; errList.AddLast(new ErrorLog(3)); Sweep(); } } public void AddErrorEmptyTimes() { lock (this) { errorEmptyTimes += 1; errorContinurousTimes += 1; errList.AddLast(new ErrorLog(0)); Sweep(); } } protected static void UpdateTransLog(List transLog, int bytes, DateTime now, ref long maxTrans, bool updateMaxTrans) { if (transLog.Count > 0) { const int base_time_diff = 100; const int max_time_diff = 3 * base_time_diff; int time_diff = (int)(now - transLog[transLog.Count - 1].recvTime).TotalMilliseconds; if (time_diff < base_time_diff) { transLog[transLog.Count - 1].times++; transLog[transLog.Count - 1].size += bytes; if (transLog[transLog.Count - 1].endTime < now) transLog[transLog.Count - 1].endTime = now; } else { if (time_diff >= 0) { transLog.Add(new TransLog(bytes, now)); int base_times = 1 + (maxTrans > 1024 * 512 ? 1 : 0); int last_index = (transLog.Count - 1) - 2; if (updateMaxTrans && transLog.Count >= 6 && transLog[last_index].times > base_times) { int begin_index = last_index - 1; for (; begin_index > 0; --begin_index) { if ((transLog[begin_index + 1].recvTime - transLog[begin_index].endTime).TotalMilliseconds > max_time_diff || transLog[begin_index].times <= base_times ) { break; } } if (begin_index <= last_index - 4) { begin_index++; TransLog t = new TransLog(transLog[begin_index].firstsize, transLog[begin_index].recvTime); t.endTime = transLog[last_index].endTime; t.size = 0; for (int i = begin_index; i <= last_index; ++i) { t.size += transLog[i].size; } if (maxTrans == 0) { maxTrans = (long)((t.size - t.firstsize) / (t.endTime - t.recvTime).TotalSeconds * 0.7); } else { double a = 2.0 / (1 + 32); maxTrans = (long)(0.5 + maxTrans * (1 - a) + a * ((t.size - t.firstsize) / (t.endTime - t.recvTime).TotalSeconds)); } } } } else { int i = transLog.Count - 1; for (; i >= 0; --i) { if (transLog[i].recvTime > now && i > 0) continue; transLog[i].times += 1; transLog[i].size += bytes; if (transLog[i].endTime < now) transLog[i].endTime = now; break; } } } while (transLog.Count > 0 && now > transLog[0].recvTime.AddSeconds(avgTime)) { transLog.RemoveAt(0); } } else { transLog.Add(new TransLog(bytes, now)); } } public void AddUploadBytes(int bytes, DateTime now, bool updateMaxTrans) { lock (this) { transUpload += bytes; UpdateTransLog(upTransLog, bytes, now, ref maxTransUpload, updateMaxTrans); } } public void AddDownloadBytes(int bytes, DateTime now, bool updateMaxTrans) { lock (this) { transDownload += bytes; UpdateTransLog(downTransLog, bytes, now, ref maxTransDownload, updateMaxTrans); } } public void AddDownloadRawBytes(long bytes) { lock (this) { transDownloadRaw += bytes; } } public void ResetErrorDecodeTimes() { lock (this) { errorDecodeTimes = 0; errorEmptyTimes = 0; errorContinurousTimes = 0; } } public void ResetContinurousTimes() { lock (this) { errorEmptyTimes = 0; errorContinurousTimes = 0; } } public void ResetEmptyTimes() { lock (this) { errorEmptyTimes = 0; } } public void AddConnectTime(int millisecond) { lock (this) { if (millisecond == 0) { millisecond = 10; } else { millisecond *= 1000; } if (avgConnectTime == -1) { avgConnectTime = millisecond; } else { double a = 2.0 / (1 + 16); avgConnectTime = (int)(0.5 + avgConnectTime * (1 - a) + a * millisecond); } } } } } ================================================ FILE: shadowsocks-csharp/Obfs/Auth.cs ================================================ using System; using System.Collections.Generic; using Shadowsocks.Controller; using System.Security.Cryptography; using Shadowsocks.Encryption; namespace Shadowsocks.Obfs { public class AuthData : VerifyData { public byte[] clientID; public UInt32 connectionID; } public class AuthSHA1 : VerifySimpleBase { public AuthSHA1(string method) : base(method) { has_sent_header = false; has_recv_header = false; } private static Dictionary _obfs = new Dictionary { {"auth_sha1", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthData(); } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = datalength > 1300 ? 1 : LinearRandomInt(64) + 1; outlength = rand_len + datalength + 6; if (datalength > 0) Array.Copy(data, 0, outdata, rand_len + 2, datalength); outdata[0] = (byte)(outlength >> 8); outdata[1] = (byte)(outlength); outdata[2] = (byte)(rand_len); ulong adler = Util.Adler32.CalcAdler32(outdata, outlength - 4); BitConverter.GetBytes((uint)adler).CopyTo(outdata, outlength - 4); } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = LinearRandomInt(250) + 1; int data_offset = rand_len + 4 + 2; outlength = data_offset + datalength + 12 + 10; AuthData authData = (AuthData)this.Server.data; lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[4]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)LinearRandomInt(0x1000000); } authData.connectionID += 1; Array.Copy(authData.clientID, 0, outdata, data_offset + 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, outdata, data_offset + 8, 4); } UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); Array.Copy(BitConverter.GetBytes(utc_time), 0, outdata, data_offset, 4); Array.Copy(data, 0, outdata, data_offset + 12, datalength); outdata[4] = (byte)(outlength >> 8); outdata[5] = (byte)(outlength); outdata[6] = (byte)(rand_len); ulong crc32 = Util.CRC32.CalcCRC32(Server.key, (int)Server.key.Length); BitConverter.GetBytes((uint)crc32).CopyTo(outdata, 0); byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); HMACSHA1 sha1 = new HMACSHA1(key); byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 10); Array.Copy(sha1data, 0, outdata, outlength - 10, 10); } public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { if (plaindata == null) { outlength = 0; return plaindata; } byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata; outlength = 0; const int unit_len = 8100; if (!has_sent_header) { int headsize = GetHeadSize(plaindata, 30); int _datalength = Math.Min(LinearRandomInt(32) + headsize, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; } while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0) { int outlen; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; while (recv_buf_len > 2) { int len = (recv_buf[0] << 8) + recv_buf[1]; if (len >= 8192 || len < 7) { throw new ObfsException("ClientPostDecrypt data error"); } if (len > recv_buf_len) break; if (Util.Adler32.CheckAdler32(recv_buf, len)) { int pos = recv_buf[2] + 2; int outlen = len - pos - 4; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(recv_buf, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len; Array.Copy(recv_buf, len, recv_buf, 0, recv_buf_len); } else { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } } return outdata; } } public class AuthSHA1V2 : VerifySimpleBase { public AuthSHA1V2(string method) : base(method) { has_sent_header = false; has_recv_header = false; } private static Dictionary _obfs = new Dictionary { {"auth_sha1_v2", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthData(); } public override bool isKeepAlive() { return true; } public override bool isAlwaysSendback() { return true; } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = (datalength >= 1300 ? 0 : (datalength > 400 ? LinearRandomInt(128) : LinearRandomInt(1024))) + 1; outlength = rand_len + datalength + 6; if (datalength > 0) Array.Copy(data, 0, outdata, rand_len + 2, datalength); outdata[0] = (byte)(outlength >> 8); outdata[1] = (byte)(outlength); { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, 2); } if (rand_len < 128) { outdata[2] = (byte)(rand_len); } else { outdata[2] = 0xFF; outdata[3] = (byte)(rand_len >> 8); outdata[4] = (byte)(rand_len); } ulong adler = Util.Adler32.CalcAdler32(outdata, outlength - 4); BitConverter.GetBytes((uint)adler).CopyTo(outdata, outlength - 4); } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = (datalength > 400 ? LinearRandomInt(128) : LinearRandomInt(1024)) + 1; int data_offset = rand_len + 4 + 2; outlength = data_offset + datalength + 12 + 10; AuthData authData = (AuthData)this.Server.data; { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, data_offset - rand_len); } lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[8]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)BitConverter.ToInt64(authData.clientID, 0) % 0xFFFFFD; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, outdata, data_offset, 8); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, outdata, data_offset + 8, 4); } Array.Copy(data, 0, outdata, data_offset + 12, datalength); outdata[4] = (byte)(outlength >> 8); outdata[5] = (byte)(outlength); if (rand_len < 128) { outdata[6] = (byte)(rand_len); } else { outdata[6] = 0xFF; outdata[7] = (byte)(rand_len >> 8); outdata[8] = (byte)(rand_len); } byte[] salt = System.Text.Encoding.UTF8.GetBytes("auth_sha1_v2"); byte[] crcdata = new byte[salt.Length + Server.key.Length]; salt.CopyTo(crcdata, 0); Server.key.CopyTo(crcdata, salt.Length); ulong crc32 = Util.CRC32.CalcCRC32(crcdata, (int)crcdata.Length); BitConverter.GetBytes((uint)crc32).CopyTo(outdata, 0); byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); HMACSHA1 sha1 = new HMACSHA1(key); byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 10); Array.Copy(sha1data, 0, outdata, outlength - 10, 10); } public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { if (plaindata == null) { outlength = 0; return plaindata; } byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata; outlength = 0; const int unit_len = 8100; int ogn_datalength = datalength; if (!has_sent_header) { int headsize = GetHeadSize(plaindata, 30); int _datalength = Math.Min(LinearRandomInt(32) + headsize, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; } while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0 || ogn_datalength == -1) { int outlen; if (ogn_datalength == -1) datalength = 0; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; while (recv_buf_len > 2) { int len = (recv_buf[0] << 8) + recv_buf[1]; if (len >= 8192 || len < 8) { throw new ObfsException("ClientPostDecrypt data error"); } if (len > recv_buf_len) break; if (Util.Adler32.CheckAdler32(recv_buf, len)) { int pos = recv_buf[2]; if (pos < 255) { pos += 2; } else { pos = ((recv_buf[3] << 8) | recv_buf[4]) + 2; } int outlen = len - pos - 4; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(recv_buf, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len; Array.Copy(recv_buf, len, recv_buf, 0, recv_buf_len); } else { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } } return outdata; } } public class AuthSHA1V4 : VerifySimpleBase { public AuthSHA1V4(string method) : base(method) { has_sent_header = false; has_recv_header = false; } private static Dictionary _obfs = new Dictionary { {"auth_sha1_v4", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); protected const string SALT = "auth_sha1_v4"; protected const int overhead = 9; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthData(); } public override bool isKeepAlive() { return true; } public override bool isAlwaysSendback() { return true; } public override int GetOverhead() { return overhead; } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = (datalength > 1200 ? 0 : (datalength > 400 ? LinearRandomInt(256) : LinearRandomInt(512))) + 1; outlength = rand_len + datalength + 8; if (datalength > 0) Array.Copy(data, 0, outdata, rand_len + 4, datalength); outdata[0] = (byte)(outlength >> 8); outdata[1] = (byte)(outlength); ulong crc32 = Util.CRC32.CalcCRC32(outdata, 2); BitConverter.GetBytes((ushort)crc32).CopyTo(outdata, 2); { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, 4); } if (rand_len < 128) { outdata[4] = (byte)(rand_len); } else { outdata[4] = 0xFF; outdata[5] = (byte)(rand_len >> 8); outdata[6] = (byte)(rand_len); } ulong adler = Util.Adler32.CalcAdler32(outdata, outlength - 4); BitConverter.GetBytes((uint)adler).CopyTo(outdata, outlength - 4); } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = (datalength > 400 ? LinearRandomInt(128) : LinearRandomInt(1024)) + 1; int data_offset = rand_len + 4 + 2; outlength = data_offset + datalength + 12 + 10; AuthData authData = (AuthData)this.Server.data; { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, data_offset - rand_len); } lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[4]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, outdata, data_offset + 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, outdata, data_offset + 8, 4); } UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); Array.Copy(BitConverter.GetBytes(utc_time), 0, outdata, data_offset, 4); Array.Copy(data, 0, outdata, data_offset + 12, datalength); outdata[0] = (byte)(outlength >> 8); outdata[1] = (byte)(outlength); if (rand_len < 128) { outdata[6] = (byte)(rand_len); } else { outdata[6] = 0xFF; outdata[7] = (byte)(rand_len >> 8); outdata[8] = (byte)(rand_len); } byte[] salt = System.Text.Encoding.UTF8.GetBytes(SALT); byte[] crcdata = new byte[salt.Length + Server.key.Length + 2]; salt.CopyTo(crcdata, 2); Server.key.CopyTo(crcdata, salt.Length + 2); crcdata[0] = outdata[0]; crcdata[1] = outdata[1]; ulong crc32 = Util.CRC32.CalcCRC32(crcdata, (int)crcdata.Length); BitConverter.GetBytes((uint)crc32).CopyTo(outdata, 2); byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); HMACSHA1 sha1 = new HMACSHA1(key); byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 10); Array.Copy(sha1data, 0, outdata, outlength - 10, 10); } public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { if (plaindata == null) { outlength = 0; return plaindata; } byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata; outlength = 0; const int unit_len = 8100; int ogn_datalength = datalength; if (!has_sent_header) { int headsize = GetHeadSize(plaindata, 30); int _datalength = Math.Min(LinearRandomInt(32) + headsize, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; } while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0 || ogn_datalength == -1) { int outlen; if (ogn_datalength == -1) datalength = 0; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; while (recv_buf_len > 4) { ulong crc32 = Util.CRC32.CalcCRC32(recv_buf, 2); if ((uint)((recv_buf[3] << 8) | recv_buf[2]) != ((uint)crc32 & 0xffff)) { throw new ObfsException("ClientPostDecrypt data error"); } int len = (recv_buf[0] << 8) + recv_buf[1]; if (len >= 8192 || len < 8) { throw new ObfsException("ClientPostDecrypt data error"); } if (len > recv_buf_len) break; if (Util.Adler32.CheckAdler32(recv_buf, len)) { int pos = recv_buf[4]; if (pos < 255) { pos += 4; } else { pos = ((recv_buf[5] << 8) | recv_buf[6]) + 4; } int outlen = len - pos - 4; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(recv_buf, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len; Array.Copy(recv_buf, len, recv_buf, 0, recv_buf_len); } else { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } } return outdata; } } public class AuthAES128SHA1 : VerifySimpleBase { protected delegate byte[] hash_func(byte[] input); protected class AuthDataAes128 : AuthData { public Model.MinSearchTree tree; } public AuthAES128SHA1(string method) : base(method) { has_sent_header = false; has_recv_header = false; pack_id = 1; recv_id = 1; SALT = method; if (method == "auth_aes128_md5") hash = MbedTLS.MD5; else hash = MbedTLS.SHA1; byte[] bytes = new byte[4]; g_random.GetBytes(bytes); random = new Random(BitConverter.ToInt32(bytes, 0)); } private static Dictionary _obfs = new Dictionary { {"auth_aes128_md5", new int[]{1, 0, 1}}, {"auth_aes128_sha1", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); protected string SALT; protected uint pack_id; protected uint recv_id; protected byte[] user_key; protected byte[] user_id; protected hash_func hash; protected byte[] send_buffer; protected int last_datalength; protected const int overhead = 9; // 2(length) + 2(len-MAC) + 4(data-MAC) + 1(padding) //protected int[] packet_cnt; protected Dictionary packet_cnt = new Dictionary(); //protected int[] packet_mul; protected Model.MinSearchTree tree; protected const int tree_offset = 9; protected DateTime lastSendTime; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthDataAes128(); } public override bool isKeepAlive() { return true; } public override bool isAlwaysSendback() { return true; } public override int GetOverhead() { return overhead; } protected MbedTLS.HMAC CreateHMAC(byte[] key) { if (Method == "auth_aes128_md5") return new MbedTLS.HMAC_MD5(key); if (Method == "auth_aes128_sha1") return new MbedTLS.HMAC_SHA1(key); return null; } protected void Sync() { #if PROTOCOL_STATISTICS if (Server.data != null) { AuthDataAes128 authData = Server.data as AuthDataAes128; lock (authData) { if (authData.tree != null && packet_cnt != null) { authData.tree.Update(packet_cnt); tree = authData.tree.Clone(); } } } #endif } // return final size, include dataLengthMax protected int RandomInMin(int dataLengthMin, int dataLengthMax) { dataLengthMin -= tree_offset; dataLengthMax -= tree_offset; int len = tree.RandomFindIndex(dataLengthMin - 1, dataLengthMax, random); tree.Update(len); return len + tree_offset + 1; } // real packet size. mapping: 1 => 0 protected void AddPacket(int length) { #if PROTOCOL_STATISTICS if (length > tree_offset) { length -= 1 + tree_offset; //if (length >= packet_cnt.Length) length = packet_cnt.Length - 1; //packet_cnt[length]++; if (length >= tree.Size) length = tree.Size - 1; if (packet_cnt.ContainsKey(length)) { packet_cnt[length] += 1; } else packet_cnt[length] = 1; } else { throw new ObfsException("AddPacket size uncorrect"); } #endif } protected void StatisticsInit(AuthDataAes128 authData) { #if PROTOCOL_STATISTICS if (authData.tree == null) { authData.tree = new Model.MinSearchTree(Server.tcp_mss - tree_offset); authData.tree.Init(); } tree = authData.tree.Clone(); #endif } protected int GetRandLen(int datalength, int fulldatalength, bool nopadding) { if (nopadding || fulldatalength >= Server.buffer_size) return 0; int rev_len = Server.tcp_mss - datalength - overhead; if (rev_len <= 0) return 0; if (datalength > 1100) return LinearRandomInt(rev_len); return TrapezoidRandomInt(rev_len, -0.3); } #if PROTOCOL_STATISTICS // packetlength + padding = real_packetlength // return size of padding, at least 1 protected int GenRandLenFull(int packetlength, int fulldatalength, bool nopadding) { if (nopadding || fulldatalength >= Server.buffer_size) return packetlength; if (packetlength >= Server.tcp_mss) { if (packetlength > Server.tcp_mss && packetlength < Server.tcp_mss * 2) { return packetlength + TrapezoidRandomInt(Server.tcp_mss * 2 - packetlength, -0.3); } return packetlength + TrapezoidRandomInt(32, -0.3); } return RandomInMin(packetlength, Server.tcp_mss - 1); } protected int GenRandLen(int packetlength, int maxpacketlength) { return RandomInMin(packetlength, maxpacketlength); } #endif public void PackData(byte[] data, int datalength, int fulldatalength, byte[] outdata, out int outlength, bool nopadding = false) { #if !PROTOCOL_STATISTICS int rand_len = GetRandLen(datalength, fulldatalength, nopadding) + 1; #else const int overhead = 8; int rand_len = GenRandLenFull((datalength == 0 ? 1 : datalength) + overhead + 1, fulldatalength, nopadding) - datalength - overhead; #endif outlength = rand_len + datalength + 8; if (datalength > 0) Array.Copy(data, 0, outdata, rand_len + 4, datalength); outdata[0] = (byte)(outlength); outdata[1] = (byte)(outlength >> 8); byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); BitConverter.GetBytes(pack_id).CopyTo(key, key.Length - 4); { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, 4); } MbedTLS.HMAC sha1 = CreateHMAC(key); { byte[] sha1data = sha1.ComputeHash(outdata, 0, 2); Array.Copy(sha1data, 0, outdata, 2, 2); } if (rand_len < 128) { outdata[4] = (byte)(rand_len); } else { outdata[4] = 0xFF; outdata[5] = (byte)(rand_len); outdata[6] = (byte)(rand_len >> 8); } ++pack_id; { byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 4); Array.Copy(sha1data, 0, outdata, outlength - 4, 4); } } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { const int authhead_len = 7 + 4 + 16 + 4; const int overhead = authhead_len + 4; byte[] encrypt = new byte[24]; AuthDataAes128 authData = this.Server.data as AuthDataAes128; lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[4]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, encrypt, 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4); StatisticsInit(authData); } #if !PROTOCOL_STATISTICS int rand_len = TrapezoidRandomInt(Server.tcp_mss - datalength - overhead + 1, -0.3); //(datalength > 400 ? LinearRandomInt(512) : LinearRandomInt(1024)); #else int rand_len = GenRandLenFull(datalength + overhead, datalength, false) - datalength - overhead; #endif int data_offset = rand_len + authhead_len; outlength = data_offset + datalength + 4; byte[] encrypt_data = new byte[32]; byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); rnd_data.CopyTo(outdata, data_offset - rand_len); } UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4); encrypt[12] = (byte)(outlength); encrypt[13] = (byte)(outlength >> 8); encrypt[14] = (byte)(rand_len); encrypt[15] = (byte)(rand_len >> 8); { byte[] uid = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = hash(System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1))); BitConverter.GetBytes(user).CopyTo(uid, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(uid); user_key = Server.key; } byte[] encrypt_key = user_key; Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT, false); int enc_outlen; encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen); encryptor.Dispose(); Array.Copy(encrypt_data, 0, encrypt, 4, 16); uid.CopyTo(encrypt, 0); } { MbedTLS.HMAC sha1 = CreateHMAC(key); byte[] sha1data = sha1.ComputeHash(encrypt, 0, 20); Array.Copy(sha1data, 0, encrypt, 20, 4); } { byte[] rnd = new byte[1]; random.NextBytes(rnd); rnd.CopyTo(outdata, 0); MbedTLS.HMAC sha1 = CreateHMAC(key); byte[] sha1data = sha1.ComputeHash(rnd, 0, rnd.Length); Array.Copy(sha1data, 0, outdata, rnd.Length, 7 - rnd.Length); } encrypt.CopyTo(outdata, 7); Array.Copy(data, 0, outdata, data_offset, datalength); { MbedTLS.HMAC sha1 = CreateHMAC(user_key); byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 4); Array.Copy(sha1data, 0, outdata, outlength - 4, 4); } } // plaindata == null try send buffer data, return null if empty buffer // datalength == 0 sendback, return 0 // datalength == -1 keepalive public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { DateTime last = lastSendTime; DateTime now = DateTime.Now; lastSendTime = now; byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata == null ? send_buffer : plaindata; outlength = 0; if (data == null) { return null; } else if (data == send_buffer) { datalength = send_buffer.Length; send_buffer = null; Sync(); last = now; } else if (send_buffer != null) { if (datalength <= 0) { return outdata; } else { Array.Resize(ref send_buffer, send_buffer.Length + datalength); Array.Copy(data, 0, send_buffer, send_buffer.Length - datalength, datalength); data = send_buffer; datalength = send_buffer.Length; send_buffer = null; } } const int unit_len = 8100; int ogn_datalength = datalength; if (datalength < 0 || last != null && (now - last).TotalSeconds > 3) { Sync(); } if (!has_sent_header) { int _datalength = Math.Min(1200, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; send_buffer = data.Length > 0 ? data : null; AddPacket(outlength); return outdata; } bool nopadding = false; #if !PROTOCOL_STATISTICS if (datalength > 120 * 4 && pack_id < 32) { { int send_len = LinearRandomInt(120 * 16); if (send_len < datalength) { send_len = TrapezoidRandomInt(Math.Min(datalength - 1, Server.tcp_mss - overhead) - 1, -0.3) + 1; // must less than datalength #else if (datalength > 120 * 4 && pack_id < 64 ) { int send_len = LinearRandomInt(datalength + 120 * 4); if (send_len < datalength) { //long min_0 = tree.GetMin(0, 512); //long min_512 = tree.GetMin(512, tree.Size); //if (min_0 < min_512) { int max_packet_size = Math.Min(datalength - 1 + overhead, Server.tcp_mss); // must less than datalength + overhead int len = GenRandLen(overhead + 1, max_packet_size) - overhead; // at least 1 byte data send_len = len; #endif send_len = datalength - send_len; if (send_len > 0) { send_buffer = new byte[send_len]; Array.Copy(data, datalength - send_len, send_buffer, 0, send_len); datalength -= send_len; } nopadding = true; } } } while (datalength > unit_len) { int outlen; PackData(data, unit_len, ogn_datalength, packdata, out outlen, nopadding); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0 || ogn_datalength == -1) { int outlen; if (ogn_datalength == -1) datalength = 0; PackData(data, datalength, ogn_datalength, packdata, out outlen, nopadding); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } last_datalength = ogn_datalength; if (outlength > 0) AddPacket(outlength); return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); while (recv_buf_len > 4) { BitConverter.GetBytes(recv_id).CopyTo(key, key.Length - 4); MbedTLS.HMAC sha1 = CreateHMAC(key); { byte[] sha1data = sha1.ComputeHash(recv_buf, 0, 2); if (sha1data[0] != recv_buf[2] || sha1data[1] != recv_buf[3]) { throw new ObfsException("ClientPostDecrypt data error"); } } int len = (recv_buf[1] << 8) + recv_buf[0]; if (len >= 8192 || len < 8) { throw new ObfsException("ClientPostDecrypt data error"); } if (len > recv_buf_len) break; { byte[] sha1data = sha1.ComputeHash(recv_buf, 0, len - 4); if (sha1data[0] != recv_buf[len - 4] || sha1data[1] != recv_buf[len - 3] || sha1data[2] != recv_buf[len - 2] || sha1data[3] != recv_buf[len - 1] ) { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } } { ++recv_id; int pos = recv_buf[4]; if (pos < 255) { pos += 4; } else { pos = ((recv_buf[6] << 8) | recv_buf[5]) + 4; } int outlen = len - pos - 4; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(recv_buf, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len; Array.Copy(recv_buf, len, recv_buf, 0, recv_buf_len); } } return outdata; } public override byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[datalength + 8]; if (user_key == null) { user_id = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = hash(System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1))); BitConverter.GetBytes(user).CopyTo(user_id, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(user_id); user_key = Server.key; } } outlength = datalength + 8; Array.Copy(plaindata, 0, outdata, 0, datalength); user_id.CopyTo(outdata, datalength); { MbedTLS.HMAC sha1 = CreateHMAC(user_key); byte[] sha1data = sha1.ComputeHash(outdata, 0, outlength - 4); Array.Copy(sha1data, 0, outdata, outlength - 4, 4); } return outdata; } public override byte[] ClientUdpPostDecrypt(byte[] plaindata, int datalength, out int outlength) { if (datalength <= 4) { outlength = 0; return plaindata; } MbedTLS.HMAC sha1 = CreateHMAC(Server.key); byte[] sha1data = sha1.ComputeHash(plaindata, 0, datalength - 4); if (sha1data[0] != plaindata[datalength - 4] || sha1data[1] != plaindata[datalength - 3] || sha1data[2] != plaindata[datalength - 2] || sha1data[3] != plaindata[datalength - 1] ) { outlength = 0; return plaindata; } outlength = datalength - 4; return plaindata; } protected override void Dispose(bool disposing) { #if PROTOCOL_STATISTICS if (disposing) { if (Server != null && Server.data != null && packet_cnt != null) { AuthDataAes128 authData = Server.data as AuthDataAes128; if (authData != null && authData.tree != null) { lock (authData) { authData.tree.Update(packet_cnt); } } } } #endif } } } ================================================ FILE: shadowsocks-csharp/Obfs/AuthAkarin.cs ================================================ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using Shadowsocks.Controller; using Shadowsocks.Encryption; namespace Shadowsocks.Obfs { class AuthAkarin : VerifySimpleBase { protected class AuthDataAesChain : AuthData { } public AuthAkarin(string method) : base(method) { has_sent_header = false; has_recv_header = false; pack_id = 1; recv_id = 1; SALT = method; byte[] bytes = new byte[4]; g_random.GetBytes(bytes); random = new Random(BitConverter.ToInt32(bytes, 0)); } private static Dictionary _obfs = new Dictionary { {"auth_akarin_rand", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); protected string SALT; protected uint pack_id; protected uint recv_id; protected byte[] user_key; protected byte[] user_id; protected byte[] send_buffer; protected int last_datalength; protected byte[] last_client_hash; protected byte[] last_server_hash; protected xorshift128plus random_client = new xorshift128plus(0); protected xorshift128plus random_server = new xorshift128plus(0); protected IEncryptor encryptor; protected int send_tcp_mss = 2000; protected int recv_tcp_mss = 2000; protected List send_back_cmd = new List(); protected const int overhead = 4; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthDataAesChain(); } public override bool isKeepAlive() { return true; } public override bool isAlwaysSendback() { return true; } public override int GetOverhead() { return overhead; } protected MbedTLS.HMAC CreateHMAC(byte[] key) { return new MbedTLS.HMAC_MD5(key); } protected virtual int GetSendRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength + Server.overhead > send_tcp_mss) { random.init_from_bin(last_hash, datalength); return (int)(random.next() % 521); } if (datalength >= 1440 || datalength + Server.overhead == recv_tcp_mss) return 0; random.init_from_bin(last_hash, datalength); if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % (ulong)(send_tcp_mss - datalength - Server.overhead)); //return (int)(random.next() % 1021); } protected virtual int GetRecvRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength + Server.overhead > recv_tcp_mss) { random.init_from_bin(last_hash, datalength); return (int)(random.next() % 521); } if (datalength >= 1440 || datalength + Server.overhead == recv_tcp_mss) return 0; random.init_from_bin(last_hash, datalength); if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % (ulong)(recv_tcp_mss - datalength - Server.overhead)); //return (int)(random.next() % 1021); } protected int UdpGetRandLen(xorshift128plus random, byte[] last_hash) { random.init_from_bin(last_hash); return (int)(random.next() % 127); } protected int GetSendRandLen(int datalength) { return GetSendRandLen(datalength, random_client, last_client_hash); } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int cmdlen = 0; int rand_len; int start_pos = 2; if (send_back_cmd.Count > 0) { cmdlen += 2; //TODO send_tcp_mss = recv_tcp_mss; rand_len = GetSendRandLen(datalength + cmdlen); outlength = rand_len + datalength + cmdlen + 2; start_pos += cmdlen; outdata[0] = (byte)(send_back_cmd[0] ^ last_client_hash[14]); outdata[1] = (byte)((send_back_cmd[0] >> 8) ^ last_client_hash[15]); outdata[2] = (byte)(datalength ^ last_client_hash[12]); outdata[3] = (byte)((datalength >> 8) ^ last_client_hash[13]); send_back_cmd.Clear(); } else { rand_len = GetSendRandLen(datalength); outlength = rand_len + datalength + 2; outdata[0] = (byte)(datalength ^ last_client_hash[14]); outdata[1] = (byte)((datalength >> 8) ^ last_client_hash[15]); } { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); encryptor.Encrypt(data, datalength, data, out datalength); if (datalength > 0) { if (rand_len > 0) { Array.Copy(data, 0, outdata, start_pos, datalength); Array.Copy(rnd_data, 0, outdata, start_pos + datalength, rand_len); } else { Array.Copy(data, 0, outdata, start_pos, datalength); } } else { rnd_data.CopyTo(outdata, start_pos); } } byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); BitConverter.GetBytes(pack_id).CopyTo(key, key.Length - 4); MbedTLS.HMAC md5 = CreateHMAC(key); ++pack_id; { byte[] md5data = md5.ComputeHash(outdata, 0, outlength); last_client_hash = md5data; Array.Copy(md5data, 0, outdata, outlength, 2); outlength += 2; } } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { const int authhead_len = 4 + 8 + 4 + 16 + 4; byte[] encrypt = new byte[24]; AuthDataAesChain authData = this.Server.data as AuthDataAesChain; lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[4]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, encrypt, 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4); } outlength = authhead_len; byte[] encrypt_data = new byte[32]; byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4); encrypt[12] = (byte)(Server.overhead); encrypt[13] = (byte)(Server.overhead >> 8); send_tcp_mss = 1024; //random.Next(1024) + 400; recv_tcp_mss = send_tcp_mss; encrypt[14] = (byte)(send_tcp_mss); encrypt[15] = (byte)(send_tcp_mss >> 8); // first 12 bytes { byte[] rnd = new byte[4]; random.NextBytes(rnd); rnd.CopyTo(outdata, 0); MbedTLS.HMAC md5 = CreateHMAC(key); byte[] md5data = md5.ComputeHash(rnd, 0, rnd.Length); last_client_hash = md5data; Array.Copy(md5data, 0, outdata, rnd.Length, 8); } // uid & 16 bytes auth data { byte[] uid = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)); BitConverter.GetBytes(user).CopyTo(uid, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(uid); user_key = Server.key; } for (int i = 0; i < 4; ++i) { uid[i] ^= last_client_hash[8 + i]; } byte[] encrypt_key = user_key; Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT, false); int enc_outlen; encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen); encryptor.Dispose(); Array.Copy(encrypt_data, 0, encrypt, 4, 16); uid.CopyTo(encrypt, 0); } // final HMAC { MbedTLS.HMAC md5 = CreateHMAC(user_key); byte[] md5data = md5.ComputeHash(encrypt, 0, 20); last_server_hash = md5data; Array.Copy(md5data, 0, encrypt, 20, 4); } encrypt.CopyTo(outdata, 12); encryptor = EncryptorFactory.GetEncryptor("chacha20", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(last_client_hash, 0, 16), false); { byte[] iv = new byte[8]; Array.Copy(last_client_hash, iv, 8); encryptor.SetIV(iv); } { int pack_outlength; encryptor.Decrypt(last_server_hash, 8, outdata, out pack_outlength); } // combine first chunk { byte[] pack_outdata = new byte[outdata.Length]; int pack_outlength; PackData(data, datalength, pack_outdata, out pack_outlength); Array.Copy(pack_outdata, 0, outdata, outlength, pack_outlength); outlength += pack_outlength; } } // plaindata == null try send buffer data, return null if empty buffer // datalength == 0 sendback, return 0 // datalength == -1 keepalive public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata == null ? send_buffer : plaindata; outlength = 0; if (data == null) { return null; } else if (data == send_buffer) { datalength = send_buffer.Length; send_buffer = null; } else if (send_buffer != null) { if (datalength <= 0) { return outdata; } else { Array.Resize(ref send_buffer, send_buffer.Length + datalength); Array.Copy(data, 0, send_buffer, send_buffer.Length - datalength, datalength); data = send_buffer; datalength = send_buffer.Length; send_buffer = null; } } int unit_len = Server.tcp_mss - Server.overhead; int ogn_datalength = datalength; if (!has_sent_header) { int _datalength = Math.Min(1200, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; send_buffer = data.Length > 0 ? data : null; return outdata; } if (datalength > 120 * 4 && pack_id < 32) { int send_len = LinearRandomInt(120 * 16); if (send_len < datalength) { send_len = TrapezoidRandomInt(Math.Min(datalength - 1, Server.tcp_mss - overhead) - 1, -0.3) + 1; // must less than datalength send_len = datalength - send_len; if (send_len > 0) { send_buffer = new byte[send_len]; Array.Copy(data, datalength - send_len, send_buffer, 0, send_len); datalength -= send_len; } } } while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0 || ogn_datalength == -1) { int outlen; if (ogn_datalength == -1) datalength = 0; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } last_datalength = ogn_datalength; return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); while (recv_buf_len > 4) { BitConverter.GetBytes(recv_id).CopyTo(key, key.Length - 4); MbedTLS.HMAC md5 = CreateHMAC(key); int data_len = ((recv_buf[1] ^ last_server_hash[15]) << 8) + (recv_buf[0] ^ last_server_hash[14]); int rand_len = GetRecvRandLen(data_len, random_server, last_server_hash); int len = rand_len + data_len; if (len >= 4096) { throw new ObfsException("ClientPostDecrypt data error"); } if (len + 4 > recv_buf_len) break; byte[] md5data = md5.ComputeHash(recv_buf, 0, len + 2); if (md5data[0] != recv_buf[len + 2] || md5data[1] != recv_buf[len + 3] ) { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } { int pos = 2; int outlen = data_len; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); byte[] data = new byte[outlen]; Array.Copy(recv_buf, pos, data, 0, outlen); encryptor.Decrypt(data, outlen, data, out outlen); last_server_hash = md5data; if (recv_id == 1) { Server.tcp_mss = recv_tcp_mss = data[0] | (data[1] << 8); pos = 2; outlen -= 2; send_back_cmd.Add(0xff00); } else { pos = 0; } Array.Copy(data, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len + 4; Array.Copy(recv_buf, len + 4, recv_buf, 0, recv_buf_len); ++recv_id; } } return outdata; } public override byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[datalength + 1024]; if (user_key == null) { user_id = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)); BitConverter.GetBytes(user).CopyTo(user_id, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(user_id); user_key = Server.key; } } byte[] auth_data = new byte[3]; random.NextBytes(auth_data); MbedTLS.HMAC md5 = CreateHMAC(Server.key); byte[] md5data = md5.ComputeHash(auth_data, 0, auth_data.Length); int rand_len = UdpGetRandLen(random_client, md5data); byte[] rand_data = new byte[rand_len]; random.NextBytes(rand_data); outlength = datalength + rand_len + 8; encryptor = EncryptorFactory.GetEncryptor("chacha20", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(md5data, 0, 16), false); { byte[] iv = new byte[8]; Array.Copy(Server.key, iv, 8); encryptor.SetIV(iv); } encryptor.Encrypt(plaindata, datalength, outdata, out datalength); rand_data.CopyTo(outdata, datalength); auth_data.CopyTo(outdata, outlength - 8); byte[] uid = new byte[4]; for (int i = 0; i < 4; ++i) { uid[i] = (byte)(user_id[i] ^ md5data[i]); } uid.CopyTo(outdata, outlength - 5); { md5 = CreateHMAC(user_key); md5data = md5.ComputeHash(outdata, 0, outlength - 1); Array.Copy(md5data, 0, outdata, outlength - 1, 1); } return outdata; } public override byte[] ClientUdpPostDecrypt(byte[] plaindata, int datalength, out int outlength) { if (datalength <= 8) { outlength = 0; return plaindata; } MbedTLS.HMAC md5 = CreateHMAC(user_key); byte[] md5data = md5.ComputeHash(plaindata, 0, datalength - 1); if (md5data[0] != plaindata[datalength - 1]) { outlength = 0; return plaindata; } md5 = CreateHMAC(Server.key); md5data = md5.ComputeHash(plaindata, datalength - 8, 7); int rand_len = UdpGetRandLen(random_server, md5data); outlength = datalength - rand_len - 8; encryptor = EncryptorFactory.GetEncryptor("chacha20", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(md5data, 0, 16), false); { int temp; byte[] iv = new byte[8]; Array.Copy(Server.key, iv, 8); encryptor.Decrypt(iv, 8, plaindata, out temp); } encryptor.Decrypt(plaindata, outlength, plaindata, out outlength); return plaindata; } } class AuthAkarin_spec_a : AuthAkarin { public AuthAkarin_spec_a(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_akarin_spec_a", new int[]{1, 0, 1}}, }; protected int[] data_size_list = null; protected int[] data_size_list2 = null; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected void InitDataSizeList() { xorshift128plus random = new xorshift128plus(0); random.init_from_bin(Server.key); int len = (int)(random.next() % 8 + 4); List data_list = new List(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); data_size_list = data_list.ToArray(); len = (int)(random.next() % 16 + 8); data_list.Clear(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); data_size_list2 = data_list.ToArray(); } public override void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; InitDataSizeList(); } protected int FindPos(int[] arr, int key) { int low = 0; int high = arr.Length - 1; int middle = -1; if (key > arr[high]) return arr.Length; while (low < high) { middle = (low + high) / 2; if (key > arr[middle]) { low = middle + 1; } else if (key <= arr[middle]) { high = middle; } } return low; } protected override int GetSendRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength + Server.overhead > send_tcp_mss) { random.init_from_bin(last_hash, datalength); return (int)(random.next() % 521); } if (datalength >= 1440 || datalength + Server.overhead == recv_tcp_mss) return 0; random.init_from_bin(last_hash, datalength); int pos = FindPos(data_size_list, datalength + Server.overhead); int final_pos = pos + (int)(random.next() % (ulong)(data_size_list.Length)); if (final_pos < data_size_list.Length) { return data_size_list[final_pos] - datalength - Server.overhead; } pos = FindPos(data_size_list2, datalength + Server.overhead); final_pos = pos + (int)(random.next() % (ulong)(data_size_list2.Length)); if (final_pos < data_size_list2.Length) { return data_size_list2[final_pos] - datalength - Server.overhead; } if (final_pos < pos + data_size_list2.Length - 1) { return 0; } if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % 1021); } protected override int GetRecvRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength + Server.overhead > recv_tcp_mss) { random.init_from_bin(last_hash, datalength); return (int)(random.next() % 521); } if (datalength >= 1440 || datalength + Server.overhead == recv_tcp_mss) return 0; random.init_from_bin(last_hash, datalength); int pos = FindPos(data_size_list, datalength + Server.overhead); int final_pos = pos + (int)(random.next() % (ulong)(data_size_list.Length)); if (final_pos < data_size_list.Length) { return data_size_list[final_pos] - datalength - Server.overhead; } pos = FindPos(data_size_list2, datalength + Server.overhead); final_pos = pos + (int)(random.next() % (ulong)(data_size_list2.Length)); if (final_pos < data_size_list2.Length) { return data_size_list2[final_pos] - datalength - Server.overhead; } if (final_pos < pos + data_size_list2.Length - 1) { return 0; } if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % 1021); } } } ================================================ FILE: shadowsocks-csharp/Obfs/AuthChain.cs ================================================ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using Shadowsocks.Controller; using Shadowsocks.Encryption; namespace Shadowsocks.Obfs { class xorshift128plus { protected UInt64 v0, v1; protected int init_loop; public xorshift128plus(int init_loop_ = 4) { v0 = v1 = 0; init_loop = init_loop_; } public UInt64 next() { UInt64 x = v0; UInt64 y = v1; v0 = y; x ^= x << 23; x ^= (y ^ (x >> 17) ^ (y >> 26)); v1 = x; return x + y; } public void init_from_bin(byte[] bytes) { byte[] fill_bytes = new byte[16]; Array.Copy(bytes, fill_bytes, 16); v0 = BitConverter.ToUInt64(fill_bytes, 0); v1 = BitConverter.ToUInt64(fill_bytes, 8); } public void init_from_bin(byte[] bytes, int datalength) { byte[] fill_bytes = new byte[16]; Array.Copy(bytes, fill_bytes, 16); BitConverter.GetBytes((ushort)datalength).CopyTo(fill_bytes, 0); v0 = BitConverter.ToUInt64(fill_bytes, 0); v1 = BitConverter.ToUInt64(fill_bytes, 8); for (int i = 0; i < init_loop; ++i) { next(); } } } class AuthChain_a : VerifySimpleBase { protected class AuthDataAesChain : AuthData { } public AuthChain_a(string method) : base(method) { has_sent_header = false; has_recv_header = false; pack_id = 1; recv_id = 1; SALT = method; byte[] bytes = new byte[4]; g_random.GetBytes(bytes); random = new Random(BitConverter.ToInt32(bytes, 0)); } private static Dictionary _obfs = new Dictionary { {"auth_chain_a", new int[]{1, 0, 1}}, }; protected bool has_sent_header; protected bool has_recv_header; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); protected string SALT; protected uint pack_id; protected uint recv_id; protected byte[] user_key; protected byte[] user_id; protected byte[] send_buffer; protected int last_datalength; protected byte[] last_client_hash; protected byte[] last_server_hash; protected xorshift128plus random_client = new xorshift128plus(); protected xorshift128plus random_server = new xorshift128plus(); protected IEncryptor encryptor; protected const int overhead = 4; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new AuthDataAesChain(); } public override bool isKeepAlive() { return true; } public override bool isAlwaysSendback() { return true; } public override int GetOverhead() { return overhead; } protected MbedTLS.HMAC CreateHMAC(byte[] key) { return new MbedTLS.HMAC_MD5(key); } protected virtual int GetRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength > 1440) return 0; random.init_from_bin(last_hash, datalength); if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % 1021); } protected int UdpGetRandLen(xorshift128plus random, byte[] last_hash) { random.init_from_bin(last_hash); return (int)(random.next() % 127); } protected int GetRandStartPos(int rand_len, xorshift128plus random) { if (rand_len > 0) return (int)(random.next() % 8589934609 % (ulong)rand_len); return 0; } protected int GetRandLen(int datalength) { return GetRandLen(datalength, random_client, last_client_hash); } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int rand_len = GetRandLen(datalength); outlength = rand_len + datalength + 2; outdata[0] = (byte)(datalength ^ last_client_hash[14]); outdata[1] = (byte)((datalength >> 8) ^ last_client_hash[15]); { byte[] rnd_data = new byte[rand_len]; random.NextBytes(rnd_data); encryptor.Encrypt(data, datalength, data, out datalength); if (datalength > 0) { if (rand_len > 0) { int start_pos = GetRandStartPos(rand_len, random_client); Array.Copy(data, 0, outdata, 2 + start_pos, datalength); Array.Copy(rnd_data, 0, outdata, 2, start_pos); Array.Copy(rnd_data, start_pos, outdata, 2 + start_pos + datalength, rand_len - start_pos); } else { Array.Copy(data, 0, outdata, 2, datalength); } } else { rnd_data.CopyTo(outdata, 2); } } byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); BitConverter.GetBytes(pack_id).CopyTo(key, key.Length - 4); MbedTLS.HMAC md5 = CreateHMAC(key); ++pack_id; { byte[] md5data = md5.ComputeHash(outdata, 0, outlength); last_client_hash = md5data; Array.Copy(md5data, 0, outdata, outlength, 2); outlength += 2; } } public virtual void OnInitAuthData(UInt64 unixTimestamp) { } public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int outlength) { const int authhead_len = 4 + 8 + 4 + 16 + 4; byte[] encrypt = new byte[24]; AuthDataAesChain authData = this.Server.data as AuthDataAesChain; lock (authData) { if (authData.connectionID > 0xFF000000) { authData.clientID = null; } if (authData.clientID == null) { authData.clientID = new byte[4]; g_random.GetBytes(authData.clientID); authData.connectionID = (UInt32)BitConverter.ToInt32(authData.clientID, 0) % 0xFFFFFD; } authData.connectionID += 1; Array.Copy(authData.clientID, 0, encrypt, 4, 4); Array.Copy(BitConverter.GetBytes(authData.connectionID), 0, encrypt, 8, 4); } outlength = authhead_len; byte[] encrypt_data = new byte[32]; byte[] key = new byte[Server.iv.Length + Server.key.Length]; Server.iv.CopyTo(key, 0); Server.key.CopyTo(key, Server.iv.Length); UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); Array.Copy(BitConverter.GetBytes(utc_time), 0, encrypt, 0, 4); OnInitAuthData(utc_time_second); encrypt[12] = (byte)(Server.overhead); encrypt[13] = (byte)(Server.overhead >> 8); // first 12 bytes { byte[] rnd = new byte[4]; random.NextBytes(rnd); rnd.CopyTo(outdata, 0); MbedTLS.HMAC md5 = CreateHMAC(key); byte[] md5data = md5.ComputeHash(rnd, 0, rnd.Length); last_client_hash = md5data; Array.Copy(md5data, 0, outdata, rnd.Length, 8); } // uid & 16 bytes auth data { byte[] uid = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)); BitConverter.GetBytes(user).CopyTo(uid, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(uid); user_key = Server.key; } for (int i = 0; i < 4; ++i) { uid[i] ^= last_client_hash[8 + i]; } byte[] encrypt_key = user_key; Encryption.IEncryptor encryptor = Encryption.EncryptorFactory.GetEncryptor("aes-128-cbc", System.Convert.ToBase64String(encrypt_key) + SALT, false); int enc_outlen; encryptor.SetIV(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); encryptor.Encrypt(encrypt, 16, encrypt_data, out enc_outlen); encryptor.Dispose(); Array.Copy(encrypt_data, 0, encrypt, 4, 16); uid.CopyTo(encrypt, 0); } // final HMAC { MbedTLS.HMAC md5 = CreateHMAC(user_key); byte[] md5data = md5.ComputeHash(encrypt, 0, 20); last_server_hash = md5data; Array.Copy(md5data, 0, encrypt, 20, 4); } encrypt.CopyTo(outdata, 12); encryptor = EncryptorFactory.GetEncryptor("rc4", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(last_client_hash, 0, 16), false); // combine first chunk { byte[] pack_outdata = new byte[outdata.Length]; int pack_outlength; PackData(data, datalength, pack_outdata, out pack_outlength); Array.Copy(pack_outdata, 0, outdata, outlength, pack_outlength); outlength += pack_outlength; } } // plaindata == null try send buffer data, return null if empty buffer // datalength == 0 sendback, return 0 // datalength == -1 keepalive public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[9000]; byte[] data = plaindata == null ? send_buffer : plaindata; outlength = 0; if (data == null) { return null; } else if (data == send_buffer) { datalength = send_buffer.Length; send_buffer = null; } else if (send_buffer != null) { if (datalength <= 0) { return outdata; } else { Array.Resize(ref send_buffer, send_buffer.Length + datalength); Array.Copy(data, 0, send_buffer, send_buffer.Length - datalength, datalength); data = send_buffer; datalength = send_buffer.Length; send_buffer = null; } } int unit_len = Server.tcp_mss - Server.overhead; int ogn_datalength = datalength; if (!has_sent_header) { int _datalength = Math.Min(1200, datalength); int outlen; PackAuthData(data, _datalength, packdata, out outlen); has_sent_header = true; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= _datalength; byte[] newdata = new byte[datalength]; Array.Copy(data, _datalength, newdata, 0, newdata.Length); data = newdata; send_buffer = data.Length > 0 ? data : null; return outdata; } if (datalength > 120 * 4 && pack_id < 32) { int send_len = LinearRandomInt(120 * 16); if (send_len < datalength) { send_len = TrapezoidRandomInt(Math.Min(datalength - 1, Server.tcp_mss - overhead) - 1, -0.3) + 1; // must less than datalength send_len = datalength - send_len; if (send_len > 0) { send_buffer = new byte[send_len]; Array.Copy(data, datalength - send_len, send_buffer, 0, send_len); datalength -= send_len; } } } while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0 || ogn_datalength == -1) { int outlen; if (ogn_datalength == -1) datalength = 0; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } last_datalength = ogn_datalength; return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; byte[] key = new byte[user_key.Length + 4]; user_key.CopyTo(key, 0); while (recv_buf_len > 4) { BitConverter.GetBytes(recv_id).CopyTo(key, key.Length - 4); MbedTLS.HMAC md5 = CreateHMAC(key); int data_len = ((recv_buf[1] ^ last_server_hash[15]) << 8) + (recv_buf[0] ^ last_server_hash[14]); int rand_len = GetRandLen(data_len, random_server, last_server_hash); int len = rand_len + data_len; if (len >= 4096) { throw new ObfsException("ClientPostDecrypt data error"); } if (len + 4 > recv_buf_len) break; byte[] md5data = md5.ComputeHash(recv_buf, 0, len + 2); if (md5data[0] != recv_buf[len + 2] || md5data[1] != recv_buf[len + 3] ) { throw new ObfsException("ClientPostDecrypt data uncorrect checksum"); } { int pos; if (data_len > 0 && rand_len > 0) { pos = 2 + GetRandStartPos(rand_len, random_server); } else { pos = 2; } int outlen = data_len; Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); byte[] data = new byte[outlen]; Array.Copy(recv_buf, pos, data, 0, outlen); encryptor.Decrypt(data, outlen, data, out outlen); last_server_hash = md5data; if (recv_id == 1) { Server.tcp_mss = data[0] | (data[1] << 8); pos = 2; outlen -= 2; } else { pos = 0; } Array.Copy(data, pos, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len + 4; Array.Copy(recv_buf, len + 4, recv_buf, 0, recv_buf_len); ++recv_id; } } return outdata; } public override byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[datalength + 1024]; if (user_key == null) { user_id = new byte[4]; int index_of_split = Server.param.IndexOf(':'); if (index_of_split > 0) { try { uint user = uint.Parse(Server.param.Substring(0, index_of_split)); user_key = System.Text.Encoding.UTF8.GetBytes(Server.param.Substring(index_of_split + 1)); BitConverter.GetBytes(user).CopyTo(user_id, 0); } catch (Exception ex) { Logging.Log(LogLevel.Warn, $"Faild to parse auth param, fallback to basic mode. {ex}"); } } if (user_key == null) { random.NextBytes(user_id); user_key = Server.key; } } byte[] auth_data = new byte[3]; random.NextBytes(auth_data); MbedTLS.HMAC md5 = CreateHMAC(Server.key); byte[] md5data = md5.ComputeHash(auth_data, 0, auth_data.Length); int rand_len = UdpGetRandLen(random_client, md5data); byte[] rand_data = new byte[rand_len]; random.NextBytes(rand_data); outlength = datalength + rand_len + 8; encryptor = EncryptorFactory.GetEncryptor("rc4", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(md5data, 0, 16), false); encryptor.Encrypt(plaindata, datalength, outdata, out datalength); rand_data.CopyTo(outdata, datalength); auth_data.CopyTo(outdata, outlength - 8); byte[] uid = new byte[4]; for (int i = 0; i < 4; ++i) { uid[i] = (byte)(user_id[i] ^ md5data[i]); } uid.CopyTo(outdata, outlength - 5); { md5 = CreateHMAC(user_key); md5data = md5.ComputeHash(outdata, 0, outlength - 1); Array.Copy(md5data, 0, outdata, outlength - 1, 1); } return outdata; } public override byte[] ClientUdpPostDecrypt(byte[] plaindata, int datalength, out int outlength) { if (datalength <= 8) { outlength = 0; return plaindata; } MbedTLS.HMAC md5 = CreateHMAC(user_key); byte[] md5data = md5.ComputeHash(plaindata, 0, datalength - 1); if (md5data[0] != plaindata[datalength - 1]) { outlength = 0; return plaindata; } md5 = CreateHMAC(Server.key); md5data = md5.ComputeHash(plaindata, datalength - 8, 7); int rand_len = UdpGetRandLen(random_server, md5data); outlength = datalength - rand_len - 8; encryptor = EncryptorFactory.GetEncryptor("rc4", System.Convert.ToBase64String(user_key) + System.Convert.ToBase64String(md5data, 0, 16), false); encryptor.Decrypt(plaindata, outlength, plaindata, out outlength); return plaindata; } } class AuthChain_b : AuthChain_a { public AuthChain_b(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_chain_b", new int[]{1, 0, 1}}, }; protected int[] data_size_list = null; protected int[] data_size_list2 = null; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected virtual void InitDataSizeList() { xorshift128plus random = new xorshift128plus(); random.init_from_bin(Server.key); int len = (int)(random.next() % 8 + 4); List data_list = new List(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); data_size_list = data_list.ToArray(); len = (int)(random.next() % 16 + 8); data_list.Clear(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); data_size_list2 = data_list.ToArray(); } public override void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; InitDataSizeList(); } protected int FindPos(int[] arr, int key) { int low = 0; int high = arr.Length - 1; int middle = -1; if (key > arr[high]) return arr.Length; while (low < high) { middle = (low + high) / 2; if (key > arr[middle]) { low = middle + 1; } else if (key <= arr[middle]) { high = middle; } } return low; } protected override int GetRandLen(int datalength, xorshift128plus random, byte[] last_hash) { if (datalength >= 1440) return 0; random.init_from_bin(last_hash, datalength); int pos = FindPos(data_size_list, datalength + Server.overhead); int final_pos = pos + (int)(random.next() % (ulong)(data_size_list.Length)); if (final_pos < data_size_list.Length) { return data_size_list[final_pos] - datalength - Server.overhead; } pos = FindPos(data_size_list2, datalength + Server.overhead); final_pos = pos + (int)(random.next() % (ulong)(data_size_list2.Length)); if (final_pos < data_size_list2.Length) { return data_size_list2[final_pos] - datalength - Server.overhead; } if (final_pos < pos + data_size_list2.Length - 1) { return 0; } if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % 1021); } } class AuthChain_c : AuthChain_b { public AuthChain_c(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_chain_c", new int[]{1, 0, 1}}, }; protected int[] data_size_list0 = null; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected override void InitDataSizeList() { xorshift128plus random = new xorshift128plus(); random.init_from_bin(Server.key); int len = (int)(random.next() % (8 + 16) + (4 + 8)); List data_list = new List(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); data_size_list0 = data_list.ToArray(); } public override void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; InitDataSizeList(); } protected override int GetRandLen(int datalength, xorshift128plus random, byte[] last_hash) { int other_data_size = datalength + Server.overhead; // 一定要在random使用前初始化,以保证服务器与客户端同步,保证包大小验证结果正确 random.init_from_bin(last_hash, datalength); if (other_data_size >= data_size_list0[data_size_list0.Length - 1]) { if (datalength >= 1440) return 0; if (datalength > 1300) return (int)(random.next() % 31); if (datalength > 900) return (int)(random.next() % 127); if (datalength > 400) return (int)(random.next() % 521); return (int)(random.next() % 1021); } int pos = FindPos(data_size_list0, other_data_size); int final_pos = pos + (int)(random.next() % (ulong)(data_size_list0.Length - pos)); return data_size_list0[final_pos] - other_data_size; } } class AuthChain_d : AuthChain_c { public AuthChain_d(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_chain_d", new int[]{1, 0, 1}}, }; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected void CheckAndPatchDataSize(List data_list, xorshift128plus random) { if (data_list[data_list.Count - 1] < 1300 && data_list.Count < 64) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); CheckAndPatchDataSize(data_list, random); } } protected override void InitDataSizeList() { xorshift128plus random = new xorshift128plus(); random.init_from_bin(Server.key); int len = (int)(random.next() % (8 + 16) + (4 + 8)); List data_list = new List(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); int old_len = data_list.Count; CheckAndPatchDataSize(data_list, random); if (old_len != data_list.Count) { data_list.Sort(); } data_size_list0 = data_list.ToArray(); } public override void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; InitDataSizeList(); } protected override int GetRandLen(int datalength, xorshift128plus random, byte[] last_hash) { int other_data_size = datalength + Server.overhead; if (other_data_size >= data_size_list0[data_size_list0.Length - 1]) { return 0; } random.init_from_bin(last_hash, datalength); int pos = FindPos(data_size_list0, other_data_size); int final_pos = pos + (int)(random.next() % (ulong)(data_size_list0.Length - pos)); return data_size_list0[final_pos] - other_data_size; } } class AuthChain_e : AuthChain_d { public AuthChain_e(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_chain_e", new int[]{1, 0, 1}}, }; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected override int GetRandLen(int datalength, xorshift128plus random, byte[] last_hash) { random.init_from_bin(last_hash, datalength); int other_data_size = datalength + Server.overhead; if (other_data_size >= data_size_list0[data_size_list0.Length - 1]) { return 0; } int pos = FindPos(data_size_list0, other_data_size); return data_size_list0[pos] - other_data_size; } } class AuthChain_f : AuthChain_e { public AuthChain_f(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"auth_chain_f", new int[]{1, 0, 1}}, }; public static new List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } protected UInt64 key_change_interval = 60 * 60 * 24; // a day by second protected UInt64 key_change_datetime_key; protected List key_change_datetime_key_bytes = new List(); protected override void InitDataSizeList() { xorshift128plus random = new xorshift128plus(); byte[] newKey = new byte[Server.key.Length]; Server.key.CopyTo(newKey, 0); for (int i = 0; i < 8; i++) { newKey[i] ^= key_change_datetime_key_bytes[i]; } random.init_from_bin(newKey); int len = (int)(random.next() % (8 + 16) + (4 + 8)); List data_list = new List(); for (int i = 0; i < len; ++i) { data_list.Add((int)(random.next() % 2340 % 2040 % 1440)); } data_list.Sort(); int old_len = data_list.Count; CheckAndPatchDataSize(data_list, random); if (old_len != data_list.Count) { data_list.Sort(); } data_size_list0 = data_list.ToArray(); } public override void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; string protocalParams = serverInfo.param; if (protocalParams != "") { if (-1 != protocalParams.IndexOf("#", StringComparison.Ordinal)) { protocalParams = protocalParams.Split('#')[1]; } UInt64 interval; if (UInt64.TryParse(protocalParams, out interval)) { key_change_interval = interval; } } } public override void OnInitAuthData(UInt64 unixTimestamp) { key_change_datetime_key = unixTimestamp / key_change_interval; for (int i = 7; i > -1; --i) { byte b = (byte)(key_change_datetime_key >> (8 * i) & 0xFF); key_change_datetime_key_bytes.Add(b); } InitDataSizeList(); } } } ================================================ FILE: shadowsocks-csharp/Obfs/HttpSimpleObfs.cs ================================================ using System; using System.Collections.Generic; using System.Security.Cryptography; namespace Shadowsocks.Obfs { class HttpSimpleObfs : ObfsBase { public HttpSimpleObfs(string method) : base(method) { has_sent_header = false; //has_recv_header = false; raw_trans_sent = false; raw_trans_recv = false; } private static Dictionary _obfs = new Dictionary { //modify original protocol, wrap protocol, obfs param {"http_simple", new int[] {0, 1, 1}}, {"http_post", new int[] {0, 1, 1}}, {"random_head", new int[] {0, 1, 0}}, }; private static string[] _request_path = new string[] { "", "", "login.php?redir=", "", "register.php?code=", "", "?keyword=", "", "search?src=typd&q=", "&lang=en", "s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&ch=&bar=&wd=", "&rn=", "post.php?id=", "&goto=view.php", }; private static string[] _request_useragent = new string[] { "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Firefox/44.0", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/27.0.1453.93 Chrome/27.0.1453.93 Safari/537.36", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:35.0) Gecko/20100101 Firefox/35.0", "Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C)", "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko", "Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/BuildID) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36", "Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", }; private static int _useragent_index = new Random().Next(_request_useragent.Length); private bool has_sent_header; //private bool has_recv_header; private bool raw_trans_sent; private bool raw_trans_recv; private List data_buffer = new List(); private Random random = new Random(); public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } private string data2urlencode(byte[] encryptdata, int datalength) { string ret = ""; for (int i = 0; i < datalength; ++i) { ret += "%" + encryptdata[i].ToString("x2"); } return ret; } private string boundary() { string set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; string ret = ""; for (int i = 0; i < 32; ++i) { ret += set[random.Next(set.Length)]; } return ret; } public override byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength) { if (raw_trans_sent) { SentLength += datalength; outlength = datalength; return encryptdata; } else { byte[] outdata = new byte[datalength + 4096]; byte[] headdata; if (Method == "random_head") { if (has_sent_header) { outlength = 0; if (datalength > 0) { byte[] data = new byte[datalength]; Array.Copy(encryptdata, 0, data, 0, datalength); data_buffer.Add(data); } else { foreach (byte[] data in data_buffer) { Array.Copy(data, 0, outdata, outlength, data.Length); SentLength += data.Length; outlength += data.Length; } data_buffer.Clear(); raw_trans_sent = true; } } else { int size = random.Next(96) + 8; byte[] rnd = new byte[size]; random.NextBytes(rnd); Util.CRC32.SetCRC32(rnd); rnd.CopyTo(outdata, 0); outlength = rnd.Length; byte[] data = new byte[datalength]; Array.Copy(encryptdata, 0, data, 0, datalength); data_buffer.Add(data); } } else if (Method == "http_simple" || Method == "http_post") { int headsize = Server.iv.Length + Server.head_len; if (datalength - headsize > 64) { headdata = new byte[headsize + random.Next(0, 64)]; } else { headdata = new byte[datalength]; } Array.Copy(encryptdata, 0, headdata, 0, headdata.Length); int request_path_index = new Random().Next(_request_path.Length / 2) * 2; string host = Server.host; string custom_head = ""; if (Server.param.Length > 0) { string[] custom_heads = Server.param.Split(new char[] { '#' }, 2); string param = Server.param; if (custom_heads.Length > 1) { custom_head = custom_heads[1]; param = custom_heads[0]; } string[] hosts = param.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (hosts != null && hosts.Length > 0) { host = hosts[random.Next(hosts.Length)]; host = host.Trim(' '); } } string http_buf = (Method == "http_post" ? "POST /" : "GET /") + _request_path[request_path_index] + data2urlencode(headdata, headdata.Length) + _request_path[request_path_index + 1] + " HTTP/1.1\r\n" + "Host: " + host + (Server.port == 80 ? "" : ":" + Server.port.ToString()) + "\r\n"; if (custom_head.Length > 0) { http_buf += custom_head.Replace("\\n", "\r\n") + "\r\n\r\n"; } else { http_buf += "User-Agent: " + _request_useragent[_useragent_index] + "\r\n" + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" + "Accept-Language: en-US,en;q=0.8\r\n" + "Accept-Encoding: gzip, deflate\r\n" + (Method == "http_post" ? "Content-Type: multipart/form-data; boundary=" + boundary() + "\r\n" : "") + "DNT: 1\r\n" + "Connection: keep-alive\r\n" + "\r\n"; } for (int i = 0; i < http_buf.Length; ++i) { outdata[i] = (byte)http_buf[i]; } if (headdata.Length < datalength) { Array.Copy(encryptdata, headdata.Length, outdata, http_buf.Length, datalength - headdata.Length); } SentLength += headdata.Length; outlength = http_buf.Length + datalength - headdata.Length; raw_trans_sent = true; } else { outlength = 0; } has_sent_header = true; return outdata; } } private int FindSubArray(byte[] array, int length, byte[] subArray) { for (int pos = 0; pos < length; ++pos) { int offset = 0; for (; offset < subArray.Length; ++offset) { if (array[pos + offset] != subArray[offset]) break; } if (offset == subArray.Length) { return pos; } } return -1; } public override byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback) { if (raw_trans_recv) { outlength = datalength; needsendback = false; return encryptdata; } else { byte[] outdata = new byte[datalength]; if (Method == "random_head") { outlength = 0; raw_trans_recv = true; needsendback = true; return encryptdata; } else { int pos = FindSubArray(encryptdata, datalength, new byte[] { (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' }); if (pos > 0) { outlength = datalength - (pos + 4); Array.Copy(encryptdata, pos + 4, outdata, 0, outlength); raw_trans_recv = true; } else { outlength = 0; } needsendback = false; } return outdata; } } } public class TlsAuthData { public byte[] clientID; public Dictionary ticket_buf; } class TlsTicketAuthObfs : ObfsBase { public TlsTicketAuthObfs(string method) : base(method) { handshake_status = 0; if (method == "tls1.2_ticket_fastauth") fastauth = true; } private static Dictionary _obfs = new Dictionary { {"tls1.2_ticket_auth", new int[] {0, 1, 1}}, {"tls1.2_ticket_fastauth", new int[] {0, 1, 1}}, }; private int handshake_status; private List data_sent_buffer = new List(); private byte[] data_recv_buffer = new byte[0]; private uint send_id = 0; private bool fastauth = false; protected static RNGCryptoServiceProvider g_random = new RNGCryptoServiceProvider(); protected Random random = new Random(); protected const int overhead = 5; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override object InitData() { return new TlsAuthData(); } public override bool isAlwaysSendback() { return true; } public override int GetOverhead() { return overhead; } protected byte[] sni(string url) { if (url == null) { url = ""; } byte[] b_url = System.Text.Encoding.UTF8.GetBytes(url); int len = b_url.Length; byte[] ret = new byte[len + 9]; Array.Copy(b_url, 0, ret, 9, len); ret[7] = (byte)(len >> 8); ret[8] = (byte)len; len += 3; ret[4] = (byte)(len >> 8); ret[5] = (byte)len; len += 2; ret[2] = (byte)(len >> 8); ret[3] = (byte)len; return ret; } protected byte to_val(char c) { if (c > '9') { return (byte)(c - 'a' + 10); } else { return (byte)(c - '0'); } } protected byte[] to_bin(string str) { byte[] ret = new byte[str.Length / 2]; for (int i = 0; i < str.Length; i += 2) { ret[i / 2] = (byte)((to_val(str[i]) << 4) | to_val(str[i + 1])); } return ret; } protected void hmac_sha1(byte[] data, int length) { byte[] key = new byte[Server.key.Length + 32]; Server.key.CopyTo(key, 0); ((TlsAuthData)this.Server.data).clientID.CopyTo(key, Server.key.Length); HMACSHA1 sha1 = new HMACSHA1(key); byte[] sha1data = sha1.ComputeHash(data, 0, length - 10); Array.Copy(sha1data, 0, data, length - 10, 10); } public void PackAuthData(byte[] outdata) { int outlength = 32; { byte[] randomdata = new byte[18]; g_random.GetBytes(randomdata); randomdata.CopyTo(outdata, 4); } TlsAuthData authData = (TlsAuthData)this.Server.data; lock (authData) { if (authData.clientID == null) { authData.clientID = new byte[32]; g_random.GetBytes(authData.clientID); } } UInt64 utc_time_second = (UInt64)Math.Floor(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); UInt32 utc_time = (UInt32)(utc_time_second); byte[] time_bytes = BitConverter.GetBytes(utc_time); Array.Reverse(time_bytes); Array.Copy(time_bytes, 0, outdata, 0, 4); hmac_sha1(outdata, outlength); } protected void PackData(byte[] data, ref int start, int len, byte[] outdata, ref int outlength) { outdata[outlength] = 0x17; outdata[outlength + 1] = 0x3; outdata[outlength + 2] = 0x3; outdata[outlength + 3] = (byte)(len >> 8); outdata[outlength + 4] = (byte)(len); Array.Copy(data, start, outdata, outlength + 5, len); start += len; outlength += len + 5; ++send_id; } public override byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength) { if (handshake_status == -1) { SentLength += datalength; outlength = datalength; return encryptdata; } byte[] outdata = new byte[datalength + 4096]; if ((handshake_status & 4) == 4) { int start = 0; outlength = 0; while (send_id <= 4 && datalength - start > 256) { int len = random.Next(512) + 64; if (len > datalength - start) len = datalength - start; PackData(encryptdata, ref start, len, outdata, ref outlength); } while (datalength - start > 2048) { int len = random.Next(4096) + 100; if (len > datalength - start) len = datalength - start; PackData(encryptdata, ref start, len, outdata, ref outlength); } if (datalength - start > 0) { PackData(encryptdata, ref start, datalength - start, outdata, ref outlength); } return outdata; } if (datalength > 0) { byte[] data = new byte[datalength + 5]; data[0] = 0x17; data[1] = 0x3; data[2] = 0x3; data[3] = (byte)(datalength >> 8); data[4] = (byte)(datalength); Array.Copy(encryptdata, 0, data, 5, datalength); data_sent_buffer.Add(data); } if ((handshake_status & 3) != 0) { outlength = 0; if ((handshake_status & 2) == 0) { int[] finish_len_set = new int[] { 32 }; //, 40, 64 int finish_len = finish_len_set[random.Next(finish_len_set.Length)]; byte[] hmac_data = new byte[11 + finish_len]; byte[] rnd = new byte[finish_len - 10]; random.NextBytes(rnd); byte[] handshake_finish = System.Text.Encoding.ASCII.GetBytes("\x14\x03\x03\x00\x01\x01" + "\x16\x03\x03\x00\x20"); handshake_finish[handshake_finish.Length - 1] = (byte)finish_len; handshake_finish.CopyTo(hmac_data, 0); rnd.CopyTo(hmac_data, handshake_finish.Length); hmac_sha1(hmac_data, hmac_data.Length); data_sent_buffer.Insert(0, hmac_data); SentLength -= hmac_data.Length; handshake_status |= 2; } if (datalength == 0 || fastauth) { foreach (byte[] data in data_sent_buffer) { Util.Utils.SetArrayMinSize2(ref outdata, outlength + data.Length); Array.Copy(data, 0, outdata, outlength, data.Length); SentLength += data.Length; outlength += data.Length; } data_sent_buffer.Clear(); } if (datalength == 0) { handshake_status |= 4; } } else { byte[] rnd = new byte[32]; PackAuthData(rnd); List ssl_buf = new List(); List ext_buf = new List(); string str_buf = "001cc02bc02fcca9cca8cc14cc13c00ac014c009c013009c0035002f000a0100"; ssl_buf.AddRange(rnd); ssl_buf.Add(32); ssl_buf.AddRange(((TlsAuthData)this.Server.data).clientID); ssl_buf.AddRange(to_bin(str_buf)); str_buf = "ff01000100"; ext_buf.AddRange(to_bin(str_buf)); string host = Server.host; if (Server.param.Length > 0) { string[] hosts = Server.param.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (hosts != null && hosts.Length > 0) { host = hosts[random.Next(hosts.Length)]; host = host.Trim(' '); } } if (!string.IsNullOrEmpty(host) && host[host.Length - 1] >= '0' && host[host.Length - 1] <= '9' && Server.param.Length == 0) { host = ""; } ext_buf.AddRange(sni(host)); string str_buf2 = "001700000023"; ext_buf.AddRange(to_bin(str_buf2)); { TlsAuthData authData = (TlsAuthData)this.Server.data; byte[] ticket = null; lock (authData) { if (authData.ticket_buf == null) { authData.ticket_buf = new Dictionary(); } if (!authData.ticket_buf.ContainsKey(host) || random.Next(16) == 0) { int ticket_size = random.Next(32, 196) * 2; ticket = new byte[ticket_size]; g_random.GetBytes(ticket); authData.ticket_buf[host] = ticket; } else { ticket = authData.ticket_buf[host]; } } ext_buf.Add((byte)(ticket.Length >> 8)); ext_buf.Add((byte)(ticket.Length & 0xff)); ext_buf.AddRange(ticket); } string str_buf3 = "000d0016001406010603050105030401040303010303020102030005000501000000000012000075500000000b00020100000a0006000400170018"; str_buf3 += "00150066000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; ext_buf.AddRange(to_bin(str_buf3)); ext_buf.Insert(0, (byte)(ext_buf.Count % 256)); ext_buf.Insert(0, (byte)((ext_buf.Count - 1) / 256)); ssl_buf.AddRange(ext_buf); // client version ssl_buf.Insert(0, (byte)3); // version ssl_buf.Insert(0, (byte)3); // length ssl_buf.Insert(0, (byte)(ssl_buf.Count % 256)); ssl_buf.Insert(0, (byte)((ssl_buf.Count - 1) / 256)); ssl_buf.Insert(0, (byte)0); ssl_buf.Insert(0, (byte)1); // client hello // length ssl_buf.Insert(0, (byte)(ssl_buf.Count % 256)); ssl_buf.Insert(0, (byte)((ssl_buf.Count - 1) / 256)); // ssl_buf.Insert(0, (byte)0x1); // version ssl_buf.Insert(0, (byte)0x3); ssl_buf.Insert(0, (byte)0x16); for (int i = 0; i < ssl_buf.Count; ++i) { outdata[i] = (byte)ssl_buf[i]; } outlength = ssl_buf.Count; handshake_status = 1; } return outdata; } public override byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback) { if (handshake_status == -1) { outlength = datalength; needsendback = false; return encryptdata; } else if ((handshake_status & 8) == 8) { Array.Resize(ref data_recv_buffer, data_recv_buffer.Length + datalength); Array.Copy(encryptdata, 0, data_recv_buffer, data_recv_buffer.Length - datalength, datalength); needsendback = false; byte[] outdata = new byte[65536]; outlength = 0; while (data_recv_buffer.Length > 5) { if (data_recv_buffer[0] != 0x17) throw new ObfsException("ClientDecode appdata error"); int len = (data_recv_buffer[3] << 8) + data_recv_buffer[4]; int pack_len = len + 5; if (pack_len > data_recv_buffer.Length) break; Array.Copy(data_recv_buffer, 5, outdata, outlength, len); outlength += len; byte[] buffer = new byte[data_recv_buffer.Length - pack_len]; Array.Copy(data_recv_buffer, pack_len, buffer, 0, buffer.Length); data_recv_buffer = buffer; } return outdata; } else { Array.Resize(ref data_recv_buffer, data_recv_buffer.Length + datalength); Array.Copy(encryptdata, 0, data_recv_buffer, data_recv_buffer.Length - datalength, datalength); outlength = 0; needsendback = false; byte[] outdata = new byte[data_recv_buffer.Length]; if (data_recv_buffer.Length >= 11 + 32 + 1 + 32) { byte[] data = new byte[32]; Array.Copy(data_recv_buffer, 11, data, 0, 22); hmac_sha1(data, data.Length); if (!Util.Utils.BitCompare(data_recv_buffer, 11 + 22, data, 22, 10)) { throw new ObfsException("ClientDecode data error: wrong sha1"); } int headerlength = data_recv_buffer.Length; data = new byte[headerlength]; Array.Copy(data_recv_buffer, 0, data, 0, headerlength - 10); hmac_sha1(data, headerlength); if (!Util.Utils.BitCompare(data_recv_buffer, headerlength - 10, data, headerlength - 10, 10)) { headerlength = 0; while (headerlength < data_recv_buffer.Length && (data_recv_buffer[headerlength] == 0x14 || data_recv_buffer[headerlength] == 0x16)) { headerlength += 5; if (headerlength >= data_recv_buffer.Length) { return encryptdata; } headerlength += (data_recv_buffer[headerlength - 2] << 8) | data_recv_buffer[headerlength - 1]; if (headerlength > data_recv_buffer.Length) { return encryptdata; } } data = new byte[headerlength]; Array.Copy(data_recv_buffer, 0, data, 0, headerlength - 10); hmac_sha1(data, headerlength); if (!Util.Utils.BitCompare(data_recv_buffer, headerlength - 10, data, headerlength - 10, 10)) { throw new ObfsException("ClientDecode data error: wrong sha1"); } } byte[] buffer = new byte[data_recv_buffer.Length - headerlength]; Array.Copy(data_recv_buffer, headerlength, buffer, 0, buffer.Length); data_recv_buffer = buffer; handshake_status |= 8; byte[] ret = ClientDecode(encryptdata, 0, out outlength, out needsendback); needsendback = true; return ret; } return encryptdata; } } } } ================================================ FILE: shadowsocks-csharp/Obfs/IObfs.cs ================================================ using System; namespace Shadowsocks.Obfs { public class ObfsException : Exception { public ObfsException(string info) : base(info) { } } public class ServerInfo { public string host; public int port; public string param; public object data; public int tcp_mss; public int overhead; public int buffer_size; public byte[] iv; public byte[] key; public string key_str; public int head_len; public ServerInfo(string host, int port, string param, object data, byte[] iv, string key_str, byte[] key, int head_len, int tcp_mss, int overhead, int buffer_size) { this.host = host; this.port = port; this.param = param; this.data = data; this.iv = iv; this.key = key; this.key_str = key_str; this.head_len = head_len; this.tcp_mss = tcp_mss; this.overhead = overhead; this.buffer_size = buffer_size; } public void SetIV(byte[] iv) { this.iv = iv; } } public interface IObfs : IDisposable { string Name(); bool isKeepAlive(); bool isAlwaysSendback(); byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength); byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength); byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback); byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength); byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength); byte[] ClientUdpPostDecrypt(byte[] plaindata, int datalength, out int outlength); object InitData(); void SetServerInfo(ServerInfo serverInfo); void SetServerInfoIV(byte[] iv); long GetSentLength(); int GetOverhead(); int GetTcpMSS(); } } ================================================ FILE: shadowsocks-csharp/Obfs/ObfsBase.cs ================================================  using System; using System.Collections.Generic; namespace Shadowsocks.Obfs { public abstract class ObfsBase: IObfs { protected ObfsBase(string method) { Method = method; } protected string Method; protected ServerInfo Server; protected long SentLength; public abstract Dictionary GetObfs(); public string Name() { return Method; } public virtual bool isKeepAlive() { return false; } public virtual bool isAlwaysSendback() { return false; } public virtual byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { outlength = datalength; return plaindata; } public abstract byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength); public abstract byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback); public virtual byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { outlength = datalength; return plaindata; } public virtual byte[] ClientUdpPreEncrypt(byte[] plaindata, int datalength, out int outlength) { outlength = datalength; return plaindata; } public virtual byte[] ClientUdpPostDecrypt(byte[] plaindata, int datalength, out int outlength) { outlength = datalength; return plaindata; } public virtual object InitData() { return null; } public virtual void SetServerInfo(ServerInfo serverInfo) { Server = serverInfo; } public virtual void SetServerInfoIV(byte[] iv) { Server.SetIV(iv); } public static int GetHeadSize(byte[] plaindata, int defaultValue) { if (plaindata == null || plaindata.Length < 2) return defaultValue; int head_type = plaindata[0] & 0x7; if (head_type == 1) return 7; if (head_type == 4) return 19; if (head_type == 3) return 4 + plaindata[1]; if (head_type == 2) return 4 + plaindata[1]; return defaultValue; } public long GetSentLength() { return SentLength; } public virtual int GetOverhead() { return 0; } public int GetTcpMSS() { return Server.tcp_mss; } #region IDisposable protected bool _disposed; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { lock (this) { if (_disposed) { return; } _disposed = true; } } #endregion } } ================================================ FILE: shadowsocks-csharp/Obfs/ObfsFactory.cs ================================================ using System; using System.Collections.Generic; using System.Reflection; namespace Shadowsocks.Obfs { public static class ObfsFactory { private static Dictionary _registeredObfs; private static Type[] _constructorTypes = new Type[] { typeof(string) }; static ObfsFactory() { _registeredObfs = new Dictionary(); foreach (string method in Plain.SupportedObfs()) { _registeredObfs.Add(method, typeof(Plain)); } foreach (string method in HttpSimpleObfs.SupportedObfs()) { _registeredObfs.Add(method, typeof(HttpSimpleObfs)); } foreach (string method in TlsTicketAuthObfs.SupportedObfs()) { _registeredObfs.Add(method, typeof(TlsTicketAuthObfs)); } foreach (string method in VerifyDeflateObfs.SupportedObfs()) { _registeredObfs.Add(method, typeof(VerifyDeflateObfs)); } #if DEBUG foreach (string method in AuthSHA1.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthSHA1)); } foreach (string method in AuthSHA1V2.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthSHA1V2)); } #endif foreach (string method in AuthSHA1V4.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthSHA1V4)); } foreach (string method in AuthAES128SHA1.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthAES128SHA1)); } foreach (string method in AuthChain_a.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_a)); } foreach (string method in AuthChain_b.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_b)); } foreach (string method in AuthChain_c.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_c)); } foreach (string method in AuthChain_d.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_d)); } foreach (string method in AuthChain_e.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_e)); } foreach (string method in AuthChain_f.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthChain_f)); } foreach (string method in AuthAkarin.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthAkarin)); } foreach (string method in AuthAkarin_spec_a.SupportedObfs()) { _registeredObfs.Add(method, typeof(AuthAkarin_spec_a)); } } public static IObfs GetObfs(string method) { if (string.IsNullOrEmpty(method)) { method = "plain"; } method = method.ToLowerInvariant(); Type t = _registeredObfs[method]; ConstructorInfo c = t.GetConstructor(_constructorTypes); IObfs result = (IObfs)c.Invoke(new object[] { method }); return result; } } } ================================================ FILE: shadowsocks-csharp/Obfs/Plain.cs ================================================ using System; using System.Collections.Generic; namespace Shadowsocks.Obfs { public class Plain : ObfsBase { public Plain(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"plain", new int[]{0, 0, 0}}, {"origin", new int[]{0, 0, 0}}, }; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public override byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength) { outlength = datalength; SentLength += outlength; return encryptdata; } public override byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback) { outlength = datalength; needsendback = false; return encryptdata; } } } ================================================ FILE: shadowsocks-csharp/Obfs/VerifySimpleObfs.cs ================================================ using System; using System.IO; using System.Collections.Generic; using Shadowsocks.Controller; using System.Security.Cryptography; namespace Shadowsocks.Obfs { public class VerifyData { } public abstract class VerifySimpleBase : ObfsBase { public VerifySimpleBase(string method) : base(method) { } protected const int RecvBufferSize = 65536 * 2; protected byte[] recv_buf = new byte[RecvBufferSize]; protected int recv_buf_len = 0; protected Random random = new Random(); public override object InitData() { return new VerifyData(); } public override void SetServerInfo(ServerInfo serverInfo) { base.SetServerInfo(serverInfo); } public int LinearRandomInt(int max) { return random.Next(max); } public int NonLinearRandomInt(int max) { int r1, r2; if ((max & 1) == 1) { int mid = (max + 1) >> 1; r1 = random.Next(mid); r2 = random.Next(mid + 1); int r = r1 + r2; if (r == max) return mid - 1; if (r < mid) return mid - r - 1; else return max - r + mid - 1; } else { int mid = max >> 1; r1 = random.Next(mid); r2 = random.Next(mid + 1); int r = r1 + r2; if (r < mid) return mid - r - 1; else return max - r + mid - 1; } } public double TrapezoidRandomFloat(double d) // -1 <= d <= 1 { if (d == 0) return random.NextDouble(); double s = random.NextDouble(); //(2dx + 2(1 - d))x/2 = s //dx^2 + (1-d)x - s = 0 double a = 1 - d; //dx^2 + ax - s = 0 //[-a + sqrt(a^2 + 4ds)] / 2d return (Math.Sqrt(a * a + 4 * d * s) - a) / (2 * d); } public int TrapezoidRandomInt(int max, double d) { double v = TrapezoidRandomFloat(d); return (int)(v * max); } public override byte[] ClientEncode(byte[] encryptdata, int datalength, out int outlength) { outlength = datalength; return encryptdata; } public override byte[] ClientDecode(byte[] encryptdata, int datalength, out int outlength, out bool needsendback) { outlength = datalength; needsendback = false; return encryptdata; } } public class VerifyDeflateObfs : VerifySimpleBase { public VerifyDeflateObfs(string method) : base(method) { } private static Dictionary _obfs = new Dictionary { {"verify_deflate", new int[]{1, 0, 1}}, }; public static List SupportedObfs() { return new List(_obfs.Keys); } public override Dictionary GetObfs() { return _obfs; } public void PackData(byte[] data, int datalength, byte[] outdata, out int outlength) { int outlen; byte[] comdata = FileManager.DeflateCompress(data, 0, datalength, out outlen); outlength = outlen + 2 + 4; outdata[0] = (byte)(outlength >> 8); outdata[1] = (byte)(outlength); Array.Copy(comdata, 0, outdata, 2, outlen); ulong adler = Util.Adler32.CalcAdler32(data, datalength); outdata[outlength - 4] = (byte)(adler >> 24); outdata[outlength - 3] = (byte)(adler >> 16); outdata[outlength - 2] = (byte)(adler >> 8); outdata[outlength - 1] = (byte)(adler); } public override byte[] ClientPreEncrypt(byte[] plaindata, int datalength, out int outlength) { if (plaindata == null) { outlength = 0; return plaindata; } byte[] outdata = new byte[datalength + datalength / 10 + 32]; byte[] packdata = new byte[32768]; byte[] data = plaindata; outlength = 0; const int unit_len = 32700; while (datalength > unit_len) { int outlen; PackData(data, unit_len, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; datalength -= unit_len; byte[] newdata = new byte[datalength]; Array.Copy(data, unit_len, newdata, 0, newdata.Length); data = newdata; } if (datalength > 0) { int outlen; PackData(data, datalength, packdata, out outlen); Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(packdata, 0, outdata, outlength, outlen); outlength += outlen; } return outdata; } public override byte[] ClientPostDecrypt(byte[] plaindata, int datalength, out int outlength) { byte[] outdata = new byte[recv_buf_len + datalength * 2 + 16]; Array.Copy(plaindata, 0, recv_buf, recv_buf_len, datalength); recv_buf_len += datalength; outlength = 0; while (recv_buf_len > 2) { int len = (recv_buf[0] << 8) + recv_buf[1]; if (len >= 32768 || len < 6) { throw new ObfsException("ClientPostDecrypt data error"); } if (len > recv_buf_len) break; int outlen; byte[] buf = FileManager.DeflateDecompress(recv_buf, 2, len - 6, out outlen); if (buf != null) { ulong alder = Util.Adler32.CalcAdler32(buf, outlen); if (recv_buf[len - 4] == (byte)(alder >> 24) && recv_buf[len - 3] == (byte)(alder >> 16) && recv_buf[len - 2] == (byte)(alder >> 8) && recv_buf[len - 1] == (byte)(alder)) { //pass } else { throw new ObfsException("ClientPostDecrypt data decompress ERROR"); } Util.Utils.SetArrayMinSize2(ref outdata, outlength + outlen); Array.Copy(buf, 0, outdata, outlength, outlen); outlength += outlen; recv_buf_len -= len; Array.Copy(recv_buf, len, recv_buf, 0, recv_buf_len); len = (recv_buf[0] << 8) + recv_buf[1]; } else { throw new ObfsException("ClientPostDecrypt data decompress ERROR"); } } return outdata; } } } ================================================ FILE: shadowsocks-csharp/Program.cs ================================================ using Shadowsocks.Controller; using Shadowsocks.Util; using System; using System.IO; using System.Threading; using System.Windows.Forms; using Microsoft.Win32; using Shadowsocks.Model; using System.Net; #if !_CONSOLE using Shadowsocks.View; #endif namespace Shadowsocks { static class Program { private static ShadowsocksController _controller; #if !_CONSOLE public static MenuViewController _viewController { get; set; } #endif /// /// 应用程序的主入口点。 /// [STAThread] static void Main(string[] args) { #if !_CONSOLE foreach (string arg in args) { if (arg == "--setautorun") { if (!AutoStartup.Switch()) { Environment.ExitCode = 1; } return; } } if (Utils.isVirusExist()) { return; } using (Mutex mutex = new Mutex(false, "Global\\ShadowsocksR_" + Application.StartupPath.GetHashCode())) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.EnableVisualStyles(); Application.ApplicationExit += Application_ApplicationExit; SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; Application.SetCompatibleTextRenderingDefault(false); if (!mutex.WaitOne(0, false)) { 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("ShadowsocksR is already running.")); return; } #endif Directory.SetCurrentDirectory(Application.StartupPath); #if !_CONSOLE int try_times = 0; while (Configuration.Load() == null) { if (try_times >= 5) return; using (InputPassword dlg = new InputPassword()) { if (dlg.ShowDialog() == DialogResult.OK) Configuration.SetPassword(dlg.password); else return; } try_times += 1; } if (try_times > 0) Logging.save_to_file = false; #endif //#if !DEBUG Logging.OpenLogFile(); //#endif _controller = new ShadowsocksController(); HostMap.Instance().LoadHostFile(); #if _DOTNET_4_0 // Enable Modern TLS when .NET 4.5+ installed. if (EnvCheck.CheckDotNet45()) ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; #endif #if !_CONSOLE _viewController = new MenuViewController(_controller); #endif _controller.Start(); #if !_CONSOLE //Util.Utils.ReleaseMemory(); Application.Run(); } #else Console.ReadLine(); _controller.Stop(); #endif } private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) { switch (e.Mode) { case PowerModes.Resume: if (_controller != null) { System.Timers.Timer timer = new System.Timers.Timer(5 * 1000); timer.Elapsed += Timer_Elapsed; timer.AutoReset = false; timer.Enabled = true; timer.Start(); } break; case PowerModes.Suspend: if (_controller != null) _controller.Stop(); break; } } private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (_controller != null) _controller.Start(); } catch (Exception ex) { Logging.LogUsefulException(ex); } finally { try { System.Timers.Timer timer = (System.Timers.Timer)sender; timer.Enabled = false; timer.Stop(); timer.Dispose(); } catch (Exception ex) { Logging.LogUsefulException(ex); } } } private static void Application_ApplicationExit(object sender, EventArgs e) { if (_controller != null) _controller.Stop(); _controller = null; } private static int exited = 0; private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { if (Interlocked.Increment(ref exited) == 1) { Logging.Log(LogLevel.Error, e.ExceptionObject != null ? e.ExceptionObject.ToString() : ""); MessageBox.Show(I18N.GetString("Unexpected error, ShadowsocksR will exit.") + Environment.NewLine + (e.ExceptionObject != null ? e.ExceptionObject.ToString() : ""), "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } } } ================================================ FILE: shadowsocks-csharp/Properties/AssemblyInfo.cs ================================================ using Shadowsocks.Controller; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // 有关程序集的常规信息通过下列属性集 // 控制。更改这些属性值可修改 // 与程序集关联的信息。 [assembly: AssemblyTitle(UpdateChecker.Name)] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct(UpdateChecker.Name)] [assembly: AssemblyCopyright(UpdateChecker.Copyright)] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // 将 ComVisible 设置为 false 使此程序集中的类型 // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, // 则将该类型上的 ComVisible 属性设置为 true。 [assembly: ComVisible(false)] // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID [assembly: Guid("f8334709-4309-436a-8bbd-6165dcf4a660")] // 程序集的版本信息由下面四个值组成: // // 主版本 // 次版本 // 内部版本号 // 修订号 // // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion(UpdateChecker.Version)] // [assembly: AssemblyFileVersion("2.0.0")] ================================================ FILE: shadowsocks-csharp/Properties/Resources.Designer.cs ================================================ //------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ namespace Shadowsocks.Properties { using System; /// /// A strongly-typed resource class, for looking up localized strings, etc. /// // This class was auto-generated by the StronglyTypedResourceBuilder // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// /// Returns the cached ResourceManager instance used by this class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Shadowsocks.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } /// /// Looks up a localized string similar to # translation for Simplified Chinese /// ///Shadowsocks=Shadowsocks /// ///# Menu items /// ///Mode=系统代理模式 ///No modify system proxy=保持当前状态不修改 ///Disable system proxy=直连模式 ///PAC=PAC 模式 ///Global=全局模式 ///Proxy rule=代理规则 ///Bypass LAN=绕过局域网 ///Bypass LAN && China=绕过局域网和大陆 ///Bypass LAN && not China=绕过局域网和非大陆 ///User custom=用户自定义 ///Disable bypass=全局 ///Servers=服务器 ///Edit servers...=编辑服务器... ///Import servers from file...=从文件导入... ///Import from clipboard SSR links...=从剪贴板SSR链接导入... ///Import from screen QRCode...=从屏幕二维码导入... ///Servers Subscribe=服务器订 [rest of string was truncated]";. /// internal static string cn { get { return ResourceManager.GetString("cn", resourceCulture); } } /// /// Looks up a localized resource of type System.Byte[]. /// internal static byte[] libsscrypto_dll { get { object obj = ResourceManager.GetObject("libsscrypto_dll", resourceCulture); return ((byte[])(obj)); } } /// /// Looks up a localized resource of type System.Byte[]. /// internal static byte[] libsscrypto64_dll { get { object obj = ResourceManager.GetObject("libsscrypto64_dll", resourceCulture); return ((byte[])(obj)); } } /// /// Looks up a localized resource of type System.Byte[]. /// internal static byte[] mgwz_dll { get { object obj = ResourceManager.GetObject("mgwz_dll", resourceCulture); return ((byte[])(obj)); } } /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// internal static System.Drawing.Bitmap ngnl { get { object obj = ResourceManager.GetObject("ngnl", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// /// Looks up a localized string similar to listen-address __PRIVOXY_BIND_IP__:__PRIVOXY_BIND_PORT__ ///show-on-task-bar 0 ///activity-animation 0 ///forward-socks5 / 127.0.0.1:__SOCKS_PORT__ . ///hide-console ///__BYPASS_ACTION__ /// ///. /// internal static string privoxy_conf { get { return ResourceManager.GetString("privoxy_conf", resourceCulture); } } /// /// Looks up a localized resource of type System.Byte[]. /// internal static byte[] privoxy_exe { get { object obj = ResourceManager.GetObject("privoxy_exe", resourceCulture); return ((byte[])(obj)); } } /// /// Looks up a localized resource of type System.Byte[]. /// internal static byte[] proxy_pac_txt { get { object obj = ResourceManager.GetObject("proxy_pac_txt", resourceCulture); return ((byte[])(obj)); } } /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// internal static System.Drawing.Bitmap ss16 { get { object obj = ResourceManager.GetObject("ss16", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// internal static System.Drawing.Bitmap ss20 { get { object obj = ResourceManager.GetObject("ss20", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// internal static System.Drawing.Bitmap ss24 { get { object obj = ResourceManager.GetObject("ss24", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// /// Looks up a localized resource of type System.Drawing.Bitmap. /// internal static System.Drawing.Bitmap ssw128 { get { object obj = ResourceManager.GetObject("ssw128", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } /// /// Looks up a localized string similar to ! Put user rules line by line in this file. ///! See https://adblockplus.org/en/filter-cheatsheet ///. /// internal static string user_rule { get { return ResourceManager.GetString("user_rule", resourceCulture); } } /// /// Looks up a localized string similar to # translation for Traditional Chinese /// ///Shadowsocks=Shadowsocks /// ///# Menu items /// ///Mode=系統代理模式 ///No modify system proxy=保持當前狀態不修改 ///Disable system proxy=直連模式 ///PAC=PAC 模式 ///Global=全局模式 ///Proxy rule=代理規則 ///Bypass LAN=繞過區域網路 ///Bypass LAN && China=繞過區域網路和大陸 ///Bypass LAN && not China=繞過區域網路和非大陸 ///User custom=用戶自定義 ///Disable bypass=全局 ///Servers=伺服器 ///Edit servers...=編輯伺服器... ///Import servers from file...=從文件導入... ///Import from clipboard SSR links...=从剪貼板SSR連結導入... ///Import from screen QRCode...=从熒幕 QR 碼導入... ///Servers Subscrib [rest of string was truncated]";. /// internal static string zh_tw { get { return ResourceManager.GetString("zh_tw", resourceCulture); } } } } ================================================ FILE: shadowsocks-csharp/Properties/Resources.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\data\cn.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 ..\Data\libsscrypto64.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\data\libsscrypto.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\Data\mgwz.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\Resources\46418775_p0.jpg;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Data\privoxy_conf.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 ..\Data\privoxy.exe.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\Data\proxy.pac.txt.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ..\Resources\ss16.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Resources\ss20.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Resources\ss24.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Resources\ssw128.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\data\user-rule.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 ..\Data\zh-tw.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 ================================================ FILE: shadowsocks-csharp/Util/Base64.cs ================================================ using System; using System.Text; namespace Shadowsocks.Util { public static class Base64 { public static string DecodeBase64(string val) { return Encoding.UTF8.GetString(DecodeBase64ToBytes(val)); } public static byte[] DecodeBase64ToBytes(string val) { var data = val.PadRight(val.Length + (4 - val.Length % 4) % 4, '='); return Convert.FromBase64String(data); } public static string EncodeUrlSafeBase64(byte[] val, bool trim) { if (trim) return Convert.ToBase64String(val).Replace('+', '-').Replace('/', '_').TrimEnd('='); else return Convert.ToBase64String(val).Replace('+', '-').Replace('/', '_'); } public static byte[] DecodeUrlSafeBase64ToBytes(string val) { var data = val.Replace('-', '+').Replace('_', '/').PadRight(val.Length + (4 - val.Length % 4) % 4, '='); return Convert.FromBase64String(data); } public static string EncodeUrlSafeBase64(string val, bool trim = true) { return EncodeUrlSafeBase64(Encoding.UTF8.GetBytes(val), trim); } public static string DecodeUrlSafeBase64(string val) { return Encoding.UTF8.GetString(DecodeUrlSafeBase64ToBytes(val)); } public static string DecodeStandardSSRUrlSafeBase64(string val) { //if (val.IndexOf('=') >= 0) // throw new FormatException(); return Encoding.UTF8.GetString(DecodeUrlSafeBase64ToBytes(val)); } } } ================================================ FILE: shadowsocks-csharp/Util/CRC.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Util { class CRC32 { protected static ulong[] Crc32Table = CreateCRC32Table(); //生成CRC32码表 public static ulong[] CreateCRC32Table() { ulong Crc; Crc32Table = new ulong[256]; int i, j; for (i = 0; i < 256; i++) { Crc = (ulong)i; for (j = 8; j > 0; j--) { if ((Crc & 1) == 1) Crc = (Crc >> 1) ^ 0xEDB88320; else Crc >>= 1; } Crc32Table[i] = Crc; } return Crc32Table; } //获取字符串的CRC32校验值 public static ulong CalcCRC32(byte[] input, int len, ulong value = 0xffffffff) { return CalcCRC32(input, 0, len, value); } public static ulong CalcCRC32(byte[] input, int index, int len, ulong value = 0xffffffff) { byte[] buffer = input; for (int i = index; i < len; i++) { value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]]; } return value ^ 0xffffffff; } public static void SetCRC32(byte[] buffer) { SetCRC32(buffer, 0, buffer.Length); } public static void SetCRC32(byte[] buffer, int length) { SetCRC32(buffer, 0, length); } public static void SetCRC32(byte[] buffer, int index, int length) { ulong crc = ~CalcCRC32(buffer, index, length - 4); buffer[length - 1] = (byte)(crc >> 24); buffer[length - 2] = (byte)(crc >> 16); buffer[length - 3] = (byte)(crc >> 8); buffer[length - 4] = (byte)(crc); } public static bool CheckCRC32(byte[] buffer, int length) { ulong crc = CalcCRC32(buffer, length); if (crc != 0xffffffffu) return false; return true; } } class Adler32 { public static ulong CalcAdler32(byte[] input, int len) { ulong a = 1; ulong b = 0; for (int i = 0; i < len; i++) { a += input[i]; b += a; } a %= 65521; b %= 65521; return (b << 16) + a; } public static bool CheckAdler32(byte[] input, int len) { ulong adler32 = CalcAdler32(input, len - 4); int checksum = (input[len - 1] << 24) | (input[len - 2] << 16) | (input[len - 3] << 8) | input[len - 4]; return (int)adler32 == checksum; } public static bool CheckAdler32(byte[] input, int len, uint xor) { ulong adler32 = CalcAdler32(input, len - 4) ^ xor; int checksum = (input[len - 1] << 24) | (input[len - 2] << 16) | (input[len - 3] << 8) | input[len - 4]; return (int)adler32 == checksum; } } } ================================================ FILE: shadowsocks-csharp/Util/EnvCheck.cs ================================================ using System; using System.Windows.Forms; using Microsoft.Win32; namespace Shadowsocks.Util { public class EnvCheck { // According to https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed // Hard code the path in Registry. private static string dotNet45Registry = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"; public static bool CheckDotNet45() { Int32 installed = Convert.ToInt32(Registry.GetValue(dotNet45Registry, "Release", 0)); if (0 == installed) return false; if (378389 <= installed) return true; return false; } } } ================================================ FILE: shadowsocks-csharp/Util/ServerName.cs ================================================ using System; using System.Collections.Generic; using System.Text; namespace Shadowsocks.Util { public static class ServerName { public static string HideServerAddr(string addr) { System.Net.IPAddress ipAddr; string serverAlterName = addr; bool parsed = System.Net.IPAddress.TryParse(addr, out ipAddr); if (parsed) { char separator; if (System.Net.Sockets.AddressFamily.InterNetwork == ipAddr.AddressFamily) separator = '.'; // IPv4 else separator = ':'; // IPv6 serverAlterName = HideAddr(addr, separator); } else { int pos = addr.IndexOf('.', 1); if (pos > 0) { serverAlterName = ("*" + addr.Substring(pos)); } } return serverAlterName; } private static string HideAddr(string addr, char separator) { string result = ""; string[] splited = addr.Split(separator); string prefix = splited[0]; string suffix = splited[splited.Length - 1]; if (0 < prefix.Length) result = (prefix + separator); result += "**"; if (0 < suffix.Length) result += (separator + suffix); return result; } } } ================================================ FILE: shadowsocks-csharp/Util/Util.cs ================================================ using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.IO.Compression; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using OpenDNS; using Shadowsocks.Controller; using Shadowsocks; using Shadowsocks.Model; namespace Shadowsocks.Util { public class Utils { private delegate IPHostEntry GetHostEntryHandler(string ip); private static LRUCache dnsBuffer = new LRUCache(); public static LRUCache DnsBuffer { get { return dnsBuffer; } } public static LRUCache LocalDnsBuffer { get { return dnsBuffer; } } static Process current_process = Process.GetCurrentProcess(); public static void ReleaseMemory() { #if !_CONSOLE // release any unused pages // making the numbers look good in task manager // this is totally nonsense in programming // but good for those users who care // making them happier with their everyday life // which is part of user experience GC.Collect(GC.MaxGeneration); GC.WaitForPendingFinalizers(); if (UIntPtr.Size == 4) { SetProcessWorkingSetSize(current_process.Handle, (UIntPtr)0xFFFFFFFF, (UIntPtr)0xFFFFFFFF); } else if (UIntPtr.Size == 8) { SetProcessWorkingSetSize(current_process.Handle, (UIntPtr)0xFFFFFFFFFFFFFFFF, (UIntPtr)0xFFFFFFFFFFFFFFFF); } #endif } public static string UnGzip(byte[] buf) { byte[] buffer = new byte[1024]; int n; using (MemoryStream sb = new MemoryStream()) { using (GZipStream input = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress, false)) { while ((n = input.Read(buffer, 0, buffer.Length)) > 0) { sb.Write(buffer, 0, n); } } return System.Text.Encoding.UTF8.GetString(sb.ToArray()); } } public static void RandBytes(byte[] buf, int length) { byte[] temp = new byte[length]; RNGCryptoServiceProvider rngServiceProvider = new RNGCryptoServiceProvider(); rngServiceProvider.GetBytes(temp); temp.CopyTo(buf, 0); } public static UInt32 RandUInt32() { byte[] temp = new byte[4]; RNGCryptoServiceProvider rngServiceProvider = new RNGCryptoServiceProvider(); rngServiceProvider.GetBytes(temp); return BitConverter.ToUInt32(temp, 0); } public static void Shuffle(IList list, Random rng) { int n = list.Count; while (n > 1) { int k = rng.Next(n); n--; T value = list[k]; list[k] = list[n]; list[n] = value; } } public static bool BitCompare(byte[] target, int target_offset, byte[] m, int m_offset, int targetLength) { for (int i = 0; i < targetLength; ++i) { if (target[target_offset + i] != m[m_offset + i]) return false; } return true; } public static int FindStr(byte[] target, int targetLength, byte[] m) { if (m.Length > 0 && targetLength >= m.Length) { for (int i = 0; i <= targetLength - m.Length; ++i) { if (target[i] == m[0]) { int j = 1; for (; j < m.Length; ++j) { if (target[i + j] != m[j]) break; } if (j >= m.Length) { return i; } } } } return -1; } public static bool isMatchSubNet(IPAddress ip, IPAddress net, int netmask) { byte[] addr = ip.GetAddressBytes(); byte[] net_addr = net.GetAddressBytes(); int i = 8, index = 0; for (; i < netmask; i += 8, index += 1) { if (addr[index] != net_addr[index]) return false; } if ((addr[index] >> (i - netmask)) != (net_addr[index] >> (i - netmask))) return false; return true; } public static bool isMatchSubNet(IPAddress ip, string netmask) { string[] mask = netmask.Split('/'); IPAddress netmask_ip = IPAddress.Parse(mask[0]); if (ip.AddressFamily == netmask_ip.AddressFamily) { try { return isMatchSubNet(ip, netmask_ip, Convert.ToInt16(mask[1])); } catch { return false; } } else { return false; } } public static bool isLocal(IPAddress ip) { byte[] addr = ip.GetAddressBytes(); if (addr.Length == 4) { string[] netmasks = new string[] { "127.0.0.0/8", "169.254.0.0/16", }; foreach (string netmask in netmasks) { if (isMatchSubNet(ip, netmask)) return true; } return false; } else if (addr.Length == 16) { string[] netmasks = new string[] { "::1/128", }; foreach (string netmask in netmasks) { if (isMatchSubNet(ip, netmask)) return true; } return false; } return true; } public static bool isLocal(Socket socket) { return isLocal(((IPEndPoint)socket.RemoteEndPoint).Address); } public static bool isLAN(IPAddress ip) { byte[] addr = ip.GetAddressBytes(); if (addr.Length == 4) { if (ip.Equals(new IPAddress(0))) return false; string[] netmasks = new string[] { "0.0.0.0/8", "10.0.0.0/8", //"100.64.0.0/10", //部分地区运营商貌似在使用这个,这个可能不安全 "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", //"192.0.0.0/24", //"192.0.2.0/24", "192.168.0.0/16", //"198.18.0.0/15", //"198.51.100.0/24", //"203.0.113.0/24", }; foreach (string netmask in netmasks) { if (isMatchSubNet(ip, netmask)) return true; } return false; } else if (addr.Length == 16) { string[] netmasks = new string[] { "::1/128", "fc00::/7", "fe80::/10", }; foreach (string netmask in netmasks) { if (isMatchSubNet(ip, netmask)) return true; } return false; } return true; } public static bool isLAN(Socket socket) { return isLAN(((IPEndPoint)socket.RemoteEndPoint).Address); } public static String GetTimestamp(DateTime value) { return value.ToString("yyyyMMddHHmmssffff"); } public static string urlDecode(string str) { string ret = ""; for (int i = 0; i < str.Length; ++i) { if (str[i] == '%' && i < str.Length - 2) { string s = str.Substring(i + 1, 2).ToLower(); int val = 0; char c1 = s[0]; char c2 = s[1]; val += (c1 < 'a') ? c1 - '0' : 10 + (c1 - 'a'); val *= 16; val += (c2 < 'a') ? c2 - '0' : 10 + (c2 - 'a'); ret += (char)val; i += 2; } else if (str[i] == '+') { ret += ' '; } else { ret += str[i]; } } return ret; } public static void SetArrayMinSize(ref T[] array, int size) { if (size > array.Length) { Array.Resize(ref array, size); } } public static void SetArrayMinSize2(ref T[] array, int size) { if (size > array.Length) { Array.Resize(ref array, size * 2); } } public static IPAddress QueryDns(string host, string dns_servers, bool IPv6_first = false) { IPAddress ret_ipAddress = null; ret_ipAddress = _QueryDns(host, dns_servers, IPv6_first); Logging.Info($"DNS query {host} answer {ret_ipAddress.ToString()}"); return ret_ipAddress; } public static IPAddress _QueryDns(string host, string dns_servers, bool IPv6_first = false) { IPAddress ret_ipAddress = null; { if (!string.IsNullOrEmpty(dns_servers)) { OpenDNS.Types[] types; if (IPv6_first) types = new Types[] { Types.AAAA, Types.A }; else types = new Types[] { Types.A, Types.AAAA }; string[] _dns_server = dns_servers.Split(','); List dns_server = new List(); List local_dns_server = new List(); foreach (string server_str in _dns_server) { IPAddress ipAddress = null; string server = server_str.Trim(' '); int index = server.IndexOf(':'); string ip = null; string port = null; if (index >= 0) { if (server.StartsWith("[")) { int ipv6_end = server.IndexOf(']', 1); if (ipv6_end >= 0) { ip = server.Substring(1, ipv6_end - 1); index = server.IndexOf(':', ipv6_end); if (index == ipv6_end + 1) { port = server.Substring(index + 1); } } } else { ip = server.Substring(0, index); port = server.Substring(index + 1); } } else { index = server.IndexOf(' '); if (index >= 0) { ip = server.Substring(0, index); port = server.Substring(index + 1); } else { ip = server; } } if (ip != null && IPAddress.TryParse(ip, out ipAddress)) { int i_port = 53; if (port != null) int.TryParse(port, out i_port); dns_server.Add(new IPEndPoint(ipAddress, i_port)); //dns_server.Add(port == null ? ip : ip + " " + port); } } for (int query_i = 0; query_i < types.Length; ++query_i) { DnsQuery dns = new DnsQuery(host, types[query_i]); dns.RecursionDesired = true; foreach (IPEndPoint server in dns_server) { dns.Servers.Add(server); } if (dns.Send()) { int count = dns.Response.Answers.Count; if (count > 0) { for (int i = 0; i < count; ++i) { if (((ResourceRecord)dns.Response.Answers[i]).Type != types[query_i]) continue; return ((OpenDNS.Address)dns.Response.Answers[i]).IP; } } } } } { try { GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry); IAsyncResult result = callback.BeginInvoke(host, null, null); if (result.AsyncWaitHandle.WaitOne(10000, true)) { IPHostEntry ipHostEntry = callback.EndInvoke(result); foreach (IPAddress ad in ipHostEntry.AddressList) { if (ad.AddressFamily == AddressFamily.InterNetwork) return ad; } foreach (IPAddress ad in ipHostEntry.AddressList) { return ad; } } } catch { } } } return ret_ipAddress; } public static string GetExecutablePath() { return System.Reflection.Assembly.GetExecutingAssembly().Location; } public static int RunAsAdmin(string Arguments) { Process process = null; ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.Verb = "runas"; processInfo.FileName = Application.ExecutablePath; processInfo.Arguments = Arguments; try { process = Process.Start(processInfo); } catch (System.ComponentModel.Win32Exception) { return -1; } if (process != null) { process.WaitForExit(); } int ret = process.ExitCode; process.Close(); return ret; } public static int GetDpiMul() { int dpi; using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { dpi = (int)graphics.DpiX; } return (dpi * 4 + 48) / 96; } public static bool isVirusExist() { int offense_count = 0; string[] virus_process = { "360Safe","ZhuDongFangYu", "2345SoftSvc","2345RTProtect", "BaiduAnSvc","BaiduHips" }; foreach (string process_name in virus_process) { offense_count += Process.GetProcessesByName(process_name).Length; } string registry_prefix = IntPtr.Size == 4 ? @"Software\Microsoft\Windows\CurrentVersion\App Paths\" : @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths\"; string[] virus_registry = { "360safe.exe","360se6.exe", "2345MPCSafe.exe","2345Explorer.exe", "baidubrowser.exe" }; foreach (string registry_name in virus_registry) { RegistryKey registry_virus = Registry.LocalMachine.OpenSubKey(registry_prefix + registry_name); if (registry_virus != null) { offense_count++; registry_virus.Close(); } } if (offense_count != 0) { //因为只有国行小白才会看到本消息,所以用中文就行了 MessageBox.Show(I18N.GetString("SSRR无法运行于安装有[360/2345/百度]产品的电脑,告辞!")); return true; } return false; } #if !_CONSOLE public enum DeviceCap { DESKTOPVERTRES = 117, DESKTOPHORZRES = 118, } public static Point GetScreenPhysicalSize() { using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { IntPtr desktop = g.GetHdc(); int PhysicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPHORZRES); int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); return new Point(PhysicalScreenWidth, PhysicalScreenHeight); } } [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetProcessWorkingSetSize(IntPtr process, UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize); #endif } } ================================================ FILE: shadowsocks-csharp/View/ConfigForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class ConfigForm { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.panel2 = new System.Windows.Forms.Panel(); this.DeleteButton = new System.Windows.Forms.Button(); this.AddButton = new System.Windows.Forms.Button(); this.PictureQRcode = new System.Windows.Forms.PictureBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.ObfsCombo = new System.Windows.Forms.ComboBox(); this.labelObfs = new System.Windows.Forms.Label(); this.ServerPortLabel = new System.Windows.Forms.Label(); this.IPTextBox = new System.Windows.Forms.TextBox(); this.NumServerPort = new System.Windows.Forms.NumericUpDown(); this.PasswordTextBox = new System.Windows.Forms.TextBox(); this.EncryptionLabel = new System.Windows.Forms.Label(); this.EncryptionSelect = new System.Windows.Forms.ComboBox(); this.TextLink = new System.Windows.Forms.TextBox(); this.RemarksTextBox = new System.Windows.Forms.TextBox(); this.ObfsUDPLabel = new System.Windows.Forms.Label(); this.CheckObfsUDP = new System.Windows.Forms.CheckBox(); this.TCPProtocolLabel = new System.Windows.Forms.Label(); this.UDPoverTCPLabel = new System.Windows.Forms.Label(); this.CheckUDPoverUDP = new System.Windows.Forms.CheckBox(); this.LabelNote = new System.Windows.Forms.Label(); this.PasswordLabel = new System.Windows.Forms.CheckBox(); this.TCPoverUDPLabel = new System.Windows.Forms.Label(); this.CheckTCPoverUDP = new System.Windows.Forms.CheckBox(); this.TCPProtocolComboBox = new System.Windows.Forms.ComboBox(); this.labelObfsParam = new System.Windows.Forms.Label(); this.TextObfsParam = new System.Windows.Forms.TextBox(); this.labelGroup = new System.Windows.Forms.Label(); this.TextGroup = new System.Windows.Forms.TextBox(); this.checkAdvSetting = new System.Windows.Forms.CheckBox(); this.labelUDPPort = new System.Windows.Forms.Label(); this.NumUDPPort = new System.Windows.Forms.NumericUpDown(); this.checkSSRLink = new System.Windows.Forms.CheckBox(); this.labelRemarks = new System.Windows.Forms.Label(); this.labelProtocolParam = new System.Windows.Forms.Label(); this.TextProtocolParam = new System.Windows.Forms.TextBox(); this.IPLabel = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel7 = new System.Windows.Forms.TableLayoutPanel(); this.LinkUpdate = new System.Windows.Forms.LinkLabel(); this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); this.DownButton = new System.Windows.Forms.Button(); this.UpButton = new System.Windows.Forms.Button(); this.tableLayoutPanel5 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.MyCancelButton = new System.Windows.Forms.Button(); this.OKButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.PictureQRcode)).BeginInit(); this.tableLayoutPanel2.SuspendLayout(); this.ServerGroupBox.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumServerPort)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NumUDPPort)).BeginInit(); this.tableLayoutPanel7.SuspendLayout(); this.tableLayoutPanel4.SuspendLayout(); this.tableLayoutPanel5.SuspendLayout(); this.tableLayoutPanel3.SuspendLayout(); this.SuspendLayout(); // // panel2 // this.panel2.Anchor = System.Windows.Forms.AnchorStyles.Top; this.panel2.AutoSize = true; this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.panel2.Location = new System.Drawing.Point(342, 200); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(0, 0); this.panel2.TabIndex = 1; // // DeleteButton // this.DeleteButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.DeleteButton.Location = new System.Drawing.Point(130, 0); this.DeleteButton.Margin = new System.Windows.Forms.Padding(0); this.DeleteButton.Name = "DeleteButton"; this.DeleteButton.Size = new System.Drawing.Size(120, 34); this.DeleteButton.TabIndex = 2; this.DeleteButton.Text = "&Delete"; this.DeleteButton.UseVisualStyleBackColor = true; this.DeleteButton.Click += new System.EventHandler(this.DeleteButton_Click); // // AddButton // this.AddButton.Location = new System.Drawing.Point(0, 0); this.AddButton.Margin = new System.Windows.Forms.Padding(0); this.AddButton.Name = "AddButton"; this.AddButton.Size = new System.Drawing.Size(120, 34); this.AddButton.TabIndex = 1; this.AddButton.Text = "&Add"; this.AddButton.UseVisualStyleBackColor = true; this.AddButton.Click += new System.EventHandler(this.AddButton_Click); // // PictureQRcode // this.PictureQRcode.Anchor = System.Windows.Forms.AnchorStyles.None; this.PictureQRcode.BackColor = System.Drawing.SystemColors.Control; this.PictureQRcode.Location = new System.Drawing.Point(4, 135); this.PictureQRcode.Margin = new System.Windows.Forms.Padding(4); this.PictureQRcode.Name = "PictureQRcode"; this.PictureQRcode.Size = new System.Drawing.Size(260, 200); this.PictureQRcode.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.PictureQRcode.TabIndex = 13; this.PictureQRcode.TabStop = false; // // ServersListBox // this.ServersListBox.HorizontalScrollbar = true; 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"; this.ServersListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; this.ServersListBox.Size = new System.Drawing.Size(250, 292); this.ServersListBox.TabIndex = 0; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); // // tableLayoutPanel2 // this.tableLayoutPanel2.AutoSize = true; this.tableLayoutPanel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel2.ColumnCount = 3; this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.Controls.Add(this.ServerGroupBox, 1, 0); this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel7, 0, 0); this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel5, 2, 0); this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 1, 1); this.tableLayoutPanel2.Location = new System.Drawing.Point(12, 13); this.tableLayoutPanel2.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; this.tableLayoutPanel2.RowCount = 2; this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.Size = new System.Drawing.Size(909, 518); this.tableLayoutPanel2.TabIndex = 7; // // ServerGroupBox // this.ServerGroupBox.AutoSize = true; this.ServerGroupBox.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ServerGroupBox.Controls.Add(this.tableLayoutPanel1); this.ServerGroupBox.Dock = System.Windows.Forms.DockStyle.Fill; this.ServerGroupBox.Location = new System.Drawing.Point(268, 0); this.ServerGroupBox.Margin = new System.Windows.Forms.Padding(12, 0, 0, 0); this.ServerGroupBox.Name = "ServerGroupBox"; this.ServerGroupBox.Size = new System.Drawing.Size(358, 476); this.ServerGroupBox.TabIndex = 20; this.ServerGroupBox.TabStop = false; this.ServerGroupBox.Text = "Server"; // // tableLayoutPanel1 // this.tableLayoutPanel1.AutoSize = true; this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.ObfsCombo, 1, 7); this.tableLayoutPanel1.Controls.Add(this.labelObfs, 0, 7); this.tableLayoutPanel1.Controls.Add(this.ServerPortLabel, 0, 2); this.tableLayoutPanel1.Controls.Add(this.IPTextBox, 1, 1); this.tableLayoutPanel1.Controls.Add(this.NumServerPort, 1, 2); this.tableLayoutPanel1.Controls.Add(this.PasswordTextBox, 1, 3); this.tableLayoutPanel1.Controls.Add(this.EncryptionLabel, 0, 4); this.tableLayoutPanel1.Controls.Add(this.EncryptionSelect, 1, 4); this.tableLayoutPanel1.Controls.Add(this.TextLink, 1, 11); this.tableLayoutPanel1.Controls.Add(this.RemarksTextBox, 1, 9); this.tableLayoutPanel1.Controls.Add(this.ObfsUDPLabel, 0, 16); this.tableLayoutPanel1.Controls.Add(this.CheckObfsUDP, 1, 16); this.tableLayoutPanel1.Controls.Add(this.TCPProtocolLabel, 0, 5); this.tableLayoutPanel1.Controls.Add(this.UDPoverTCPLabel, 0, 15); this.tableLayoutPanel1.Controls.Add(this.CheckUDPoverUDP, 1, 15); this.tableLayoutPanel1.Controls.Add(this.LabelNote, 1, 12); this.tableLayoutPanel1.Controls.Add(this.PasswordLabel, 0, 3); this.tableLayoutPanel1.Controls.Add(this.TCPoverUDPLabel, 0, 14); this.tableLayoutPanel1.Controls.Add(this.CheckTCPoverUDP, 1, 14); this.tableLayoutPanel1.Controls.Add(this.TCPProtocolComboBox, 1, 5); this.tableLayoutPanel1.Controls.Add(this.labelObfsParam, 0, 8); this.tableLayoutPanel1.Controls.Add(this.TextObfsParam, 1, 8); this.tableLayoutPanel1.Controls.Add(this.labelGroup, 0, 10); this.tableLayoutPanel1.Controls.Add(this.TextGroup, 1, 10); this.tableLayoutPanel1.Controls.Add(this.checkAdvSetting, 0, 12); this.tableLayoutPanel1.Controls.Add(this.labelUDPPort, 0, 13); this.tableLayoutPanel1.Controls.Add(this.NumUDPPort, 1, 13); this.tableLayoutPanel1.Controls.Add(this.checkSSRLink, 0, 11); this.tableLayoutPanel1.Controls.Add(this.labelRemarks, 0, 9); this.tableLayoutPanel1.Controls.Add(this.labelProtocolParam, 0, 6); this.tableLayoutPanel1.Controls.Add(this.TextProtocolParam, 1, 6); this.tableLayoutPanel1.Controls.Add(this.IPLabel, 0, 1); this.tableLayoutPanel1.Location = new System.Drawing.Point(8, 32); this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(3); this.tableLayoutPanel1.RowCount = 18; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.Size = new System.Drawing.Size(347, 427); this.tableLayoutPanel1.TabIndex = 0; // // ObfsCombo // this.ObfsCombo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.ObfsCombo.FormattingEnabled = true; this.ObfsCombo.Items.AddRange(new object[] { "plain", "http_simple", "http_post", "random_head", "tls1.2_ticket_auth", "tls1.2_ticket_fastauth"}); this.ObfsCombo.Location = new System.Drawing.Point(108, 174); this.ObfsCombo.Margin = new System.Windows.Forms.Padding(3, 3, 3, 7); this.ObfsCombo.Name = "ObfsCombo"; this.ObfsCombo.Size = new System.Drawing.Size(233, 20); this.ObfsCombo.TabIndex = 19; this.ObfsCombo.TextChanged += new System.EventHandler(this.ObfsCombo_TextChanged); // // labelObfs // this.labelObfs.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelObfs.AutoSize = true; this.labelObfs.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.labelObfs.Location = new System.Drawing.Point(73, 180); this.labelObfs.Name = "labelObfs"; this.labelObfs.Size = new System.Drawing.Size(29, 12); this.labelObfs.TabIndex = 18; this.labelObfs.Text = "Obfs"; // // ServerPortLabel // this.ServerPortLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ServerPortLabel.AutoSize = true; this.ServerPortLabel.Location = new System.Drawing.Point(31, 37); this.ServerPortLabel.Name = "ServerPortLabel"; this.ServerPortLabel.Size = new System.Drawing.Size(71, 12); this.ServerPortLabel.TabIndex = 8; this.ServerPortLabel.Text = "Server Port"; // // IPTextBox // this.IPTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.IPTextBox.Location = new System.Drawing.Point(108, 6); this.IPTextBox.MaxLength = 512; this.IPTextBox.Name = "IPTextBox"; this.IPTextBox.Size = new System.Drawing.Size(233, 21); this.IPTextBox.TabIndex = 7; this.IPTextBox.UseSystemPasswordChar = true; this.IPTextBox.WordWrap = false; // // NumServerPort // this.NumServerPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.NumServerPort.Location = new System.Drawing.Point(108, 33); this.NumServerPort.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumServerPort.Name = "NumServerPort"; this.NumServerPort.Size = new System.Drawing.Size(233, 21); this.NumServerPort.TabIndex = 9; // // PasswordTextBox // this.PasswordTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.PasswordTextBox.Location = new System.Drawing.Point(108, 60); this.PasswordTextBox.MaxLength = 256; this.PasswordTextBox.Name = "PasswordTextBox"; this.PasswordTextBox.Size = new System.Drawing.Size(233, 21); this.PasswordTextBox.TabIndex = 11; this.PasswordTextBox.UseSystemPasswordChar = true; this.PasswordTextBox.WordWrap = false; // // EncryptionLabel // this.EncryptionLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.EncryptionLabel.AutoSize = true; this.EncryptionLabel.Location = new System.Drawing.Point(37, 93); this.EncryptionLabel.Name = "EncryptionLabel"; this.EncryptionLabel.Size = new System.Drawing.Size(65, 12); this.EncryptionLabel.TabIndex = 12; this.EncryptionLabel.Text = "Encryption"; // // EncryptionSelect // this.EncryptionSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.EncryptionSelect.FormattingEnabled = true; this.EncryptionSelect.Location = new System.Drawing.Point(108, 87); this.EncryptionSelect.Margin = new System.Windows.Forms.Padding(3, 3, 3, 7); this.EncryptionSelect.Name = "EncryptionSelect"; this.EncryptionSelect.Size = new System.Drawing.Size(233, 20); this.EncryptionSelect.TabIndex = 13; // // TextLink // this.TextLink.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.TextLink.Location = new System.Drawing.Point(108, 285); this.TextLink.Name = "TextLink"; this.TextLink.ReadOnly = true; this.TextLink.Size = new System.Drawing.Size(233, 21); this.TextLink.TabIndex = 27; this.TextLink.WordWrap = false; this.TextLink.Enter += new System.EventHandler(this.TextBox_Enter); this.TextLink.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseUp); // // RemarksTextBox // this.RemarksTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.RemarksTextBox.Location = new System.Drawing.Point(108, 231); this.RemarksTextBox.MaxLength = 256; this.RemarksTextBox.Name = "RemarksTextBox"; this.RemarksTextBox.Size = new System.Drawing.Size(233, 21); this.RemarksTextBox.TabIndex = 23; this.RemarksTextBox.WordWrap = false; // // ObfsUDPLabel // this.ObfsUDPLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ObfsUDPLabel.AutoSize = true; this.ObfsUDPLabel.Location = new System.Drawing.Point(49, 407); this.ObfsUDPLabel.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5); this.ObfsUDPLabel.Name = "ObfsUDPLabel"; this.ObfsUDPLabel.Size = new System.Drawing.Size(53, 12); this.ObfsUDPLabel.TabIndex = 36; this.ObfsUDPLabel.Text = "Obfs UDP"; this.ObfsUDPLabel.Visible = false; // // CheckObfsUDP // this.CheckObfsUDP.AutoSize = true; this.CheckObfsUDP.Location = new System.Drawing.Point(108, 405); this.CheckObfsUDP.Name = "CheckObfsUDP"; this.CheckObfsUDP.Size = new System.Drawing.Size(126, 16); this.CheckObfsUDP.TabIndex = 37; this.CheckObfsUDP.Text = "Recommend checked"; this.CheckObfsUDP.UseVisualStyleBackColor = true; this.CheckObfsUDP.Visible = false; // // TCPProtocolLabel // this.TCPProtocolLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.TCPProtocolLabel.AutoSize = true; this.TCPProtocolLabel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.TCPProtocolLabel.Location = new System.Drawing.Point(49, 123); this.TCPProtocolLabel.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5); this.TCPProtocolLabel.Name = "TCPProtocolLabel"; this.TCPProtocolLabel.Size = new System.Drawing.Size(53, 12); this.TCPProtocolLabel.TabIndex = 14; this.TCPProtocolLabel.Text = "Protocol"; // // UDPoverTCPLabel // this.UDPoverTCPLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.UDPoverTCPLabel.AutoSize = true; this.UDPoverTCPLabel.Location = new System.Drawing.Point(25, 385); this.UDPoverTCPLabel.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5); this.UDPoverTCPLabel.Name = "UDPoverTCPLabel"; this.UDPoverTCPLabel.Size = new System.Drawing.Size(77, 12); this.UDPoverTCPLabel.TabIndex = 34; this.UDPoverTCPLabel.Text = "UDP over TCP"; this.UDPoverTCPLabel.Visible = false; // // CheckUDPoverUDP // this.CheckUDPoverUDP.AutoSize = true; this.CheckUDPoverUDP.Location = new System.Drawing.Point(108, 383); this.CheckUDPoverUDP.Name = "CheckUDPoverUDP"; this.CheckUDPoverUDP.Size = new System.Drawing.Size(186, 16); this.CheckUDPoverUDP.TabIndex = 35; this.CheckUDPoverUDP.Text = "UDP over UDP if not checked"; this.CheckUDPoverUDP.UseVisualStyleBackColor = true; this.CheckUDPoverUDP.Visible = false; // // LabelNote // this.LabelNote.Anchor = System.Windows.Forms.AnchorStyles.Left; this.LabelNote.AutoSize = true; this.LabelNote.Location = new System.Drawing.Point(110, 314); this.LabelNote.Margin = new System.Windows.Forms.Padding(5); this.LabelNote.Name = "LabelNote"; this.LabelNote.Size = new System.Drawing.Size(179, 12); this.LabelNote.TabIndex = 29; this.LabelNote.Text = "NOT all server support belows"; // // PasswordLabel // this.PasswordLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.PasswordLabel.AutoSize = true; this.PasswordLabel.Location = new System.Drawing.Point(30, 62); this.PasswordLabel.Name = "PasswordLabel"; this.PasswordLabel.Size = new System.Drawing.Size(72, 16); this.PasswordLabel.TabIndex = 10; this.PasswordLabel.Text = "Password"; this.PasswordLabel.UseVisualStyleBackColor = true; this.PasswordLabel.CheckedChanged += new System.EventHandler(this.PasswordLabel_CheckedChanged); // // TCPoverUDPLabel // this.TCPoverUDPLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.TCPoverUDPLabel.AutoSize = true; this.TCPoverUDPLabel.Location = new System.Drawing.Point(25, 363); this.TCPoverUDPLabel.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5); this.TCPoverUDPLabel.Name = "TCPoverUDPLabel"; this.TCPoverUDPLabel.Size = new System.Drawing.Size(77, 12); this.TCPoverUDPLabel.TabIndex = 32; this.TCPoverUDPLabel.Text = "TCP over UDP"; this.TCPoverUDPLabel.Visible = false; // // CheckTCPoverUDP // this.CheckTCPoverUDP.AutoSize = true; this.CheckTCPoverUDP.Location = new System.Drawing.Point(108, 361); this.CheckTCPoverUDP.Name = "CheckTCPoverUDP"; this.CheckTCPoverUDP.Size = new System.Drawing.Size(186, 16); this.CheckTCPoverUDP.TabIndex = 33; this.CheckTCPoverUDP.Text = "TCP over TCP if not checked"; this.CheckTCPoverUDP.UseVisualStyleBackColor = true; this.CheckTCPoverUDP.Visible = false; // // TCPProtocolComboBox // this.TCPProtocolComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.TCPProtocolComboBox.FormattingEnabled = true; this.TCPProtocolComboBox.Items.AddRange(new object[] { "origin", "verify_deflate", "auth_sha1_v4", "auth_aes128_md5", "auth_aes128_sha1", "auth_chain_a", "auth_chain_b", "auth_chain_c", "auth_chain_d", "auth_chain_e", "auth_chain_f", "auth_akarin_rand", "auth_akarin_spec_a" }); this.TCPProtocolComboBox.Location = new System.Drawing.Point(108, 117); this.TCPProtocolComboBox.Margin = new System.Windows.Forms.Padding(3, 3, 3, 7); this.TCPProtocolComboBox.Name = "TCPProtocolComboBox"; this.TCPProtocolComboBox.Size = new System.Drawing.Size(233, 20); this.TCPProtocolComboBox.TabIndex = 15; // // labelObfsParam // this.labelObfsParam.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelObfsParam.AutoSize = true; this.labelObfsParam.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.labelObfsParam.Location = new System.Drawing.Point(37, 208); this.labelObfsParam.Name = "labelObfsParam"; this.labelObfsParam.Size = new System.Drawing.Size(65, 12); this.labelObfsParam.TabIndex = 20; this.labelObfsParam.Text = "Obfs param"; // // TextObfsParam // this.TextObfsParam.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.TextObfsParam.Location = new System.Drawing.Point(108, 204); this.TextObfsParam.Name = "TextObfsParam"; this.TextObfsParam.Size = new System.Drawing.Size(233, 21); this.TextObfsParam.TabIndex = 21; // // labelGroup // this.labelGroup.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelGroup.AutoSize = true; this.labelGroup.Location = new System.Drawing.Point(67, 262); this.labelGroup.Name = "labelGroup"; this.labelGroup.Size = new System.Drawing.Size(35, 12); this.labelGroup.TabIndex = 24; this.labelGroup.Text = "Group"; // // TextGroup // this.TextGroup.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.TextGroup.Location = new System.Drawing.Point(108, 258); this.TextGroup.MaxLength = 64; this.TextGroup.Name = "TextGroup"; this.TextGroup.Size = new System.Drawing.Size(233, 21); this.TextGroup.TabIndex = 25; this.TextGroup.WordWrap = false; // // checkAdvSetting // this.checkAdvSetting.AutoSize = true; this.checkAdvSetting.Location = new System.Drawing.Point(6, 312); this.checkAdvSetting.Name = "checkAdvSetting"; this.checkAdvSetting.Size = new System.Drawing.Size(96, 16); this.checkAdvSetting.TabIndex = 28; this.checkAdvSetting.Text = "Adv. Setting"; this.checkAdvSetting.UseVisualStyleBackColor = true; this.checkAdvSetting.CheckedChanged += new System.EventHandler(this.checkAdvSetting_CheckedChanged); // // labelUDPPort // this.labelUDPPort.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelUDPPort.AutoSize = true; this.labelUDPPort.Location = new System.Drawing.Point(49, 338); this.labelUDPPort.Margin = new System.Windows.Forms.Padding(3, 5, 3, 5); this.labelUDPPort.Name = "labelUDPPort"; this.labelUDPPort.Size = new System.Drawing.Size(53, 12); this.labelUDPPort.TabIndex = 30; this.labelUDPPort.Text = "UDP Port"; this.labelUDPPort.Visible = false; // // NumUDPPort // this.NumUDPPort.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.NumUDPPort.Location = new System.Drawing.Point(108, 334); this.NumUDPPort.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumUDPPort.Name = "NumUDPPort"; this.NumUDPPort.Size = new System.Drawing.Size(233, 21); this.NumUDPPort.TabIndex = 31; this.NumUDPPort.Visible = false; // // checkSSRLink // this.checkSSRLink.Anchor = System.Windows.Forms.AnchorStyles.Right; this.checkSSRLink.AutoSize = true; this.checkSSRLink.Checked = true; this.checkSSRLink.CheckState = System.Windows.Forms.CheckState.Checked; this.checkSSRLink.Location = new System.Drawing.Point(30, 287); this.checkSSRLink.Name = "checkSSRLink"; this.checkSSRLink.Size = new System.Drawing.Size(72, 16); this.checkSSRLink.TabIndex = 26; this.checkSSRLink.Text = "SSR Link"; this.checkSSRLink.UseVisualStyleBackColor = true; this.checkSSRLink.CheckedChanged += new System.EventHandler(this.checkSSRLink_CheckedChanged); // // labelRemarks // this.labelRemarks.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelRemarks.AutoSize = true; this.labelRemarks.Location = new System.Drawing.Point(55, 235); this.labelRemarks.Name = "labelRemarks"; this.labelRemarks.Size = new System.Drawing.Size(47, 12); this.labelRemarks.TabIndex = 22; this.labelRemarks.Text = "Remarks"; // // labelProtocolParam // this.labelProtocolParam.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelProtocolParam.AutoSize = true; this.labelProtocolParam.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.labelProtocolParam.Location = new System.Drawing.Point(13, 151); this.labelProtocolParam.Name = "labelProtocolParam"; this.labelProtocolParam.Size = new System.Drawing.Size(89, 12); this.labelProtocolParam.TabIndex = 16; this.labelProtocolParam.Text = "Protocol param"; // // TextProtocolParam // this.TextProtocolParam.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.TextProtocolParam.Location = new System.Drawing.Point(108, 147); this.TextProtocolParam.Name = "TextProtocolParam"; this.TextProtocolParam.Size = new System.Drawing.Size(233, 21); this.TextProtocolParam.TabIndex = 17; // // IPLabel // this.IPLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.IPLabel.AutoSize = true; this.IPLabel.Location = new System.Drawing.Point(24, 8); this.IPLabel.Name = "IPLabel"; this.IPLabel.Size = new System.Drawing.Size(78, 16); this.IPLabel.TabIndex = 38; this.IPLabel.Text = "Server IP"; this.IPLabel.UseVisualStyleBackColor = true; this.IPLabel.CheckedChanged += new System.EventHandler(this.IPLabel_CheckedChanged); // // tableLayoutPanel7 // this.tableLayoutPanel7.AutoSize = true; this.tableLayoutPanel7.ColumnCount = 1; this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel7.Controls.Add(this.ServersListBox, 0, 0); this.tableLayoutPanel7.Controls.Add(this.LinkUpdate, 0, 2); this.tableLayoutPanel7.Controls.Add(this.tableLayoutPanel4, 0, 1); this.tableLayoutPanel7.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel7.Location = new System.Drawing.Point(3, 3); this.tableLayoutPanel7.Name = "tableLayoutPanel7"; this.tableLayoutPanel7.RowCount = 3; this.tableLayoutPanel2.SetRowSpan(this.tableLayoutPanel7, 2); this.tableLayoutPanel7.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel7.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel7.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel7.Size = new System.Drawing.Size(250, 512); this.tableLayoutPanel7.TabIndex = 16; // // LinkUpdate // this.LinkUpdate.Anchor = System.Windows.Forms.AnchorStyles.None; this.LinkUpdate.AutoSize = true; this.LinkUpdate.Location = new System.Drawing.Point(59, 432); this.LinkUpdate.Margin = new System.Windows.Forms.Padding(5); this.LinkUpdate.Name = "LinkUpdate"; this.LinkUpdate.Size = new System.Drawing.Size(131, 12); this.LinkUpdate.TabIndex = 5; this.LinkUpdate.TabStop = true; this.LinkUpdate.Text = "New version available"; this.LinkUpdate.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkUpdate_LinkClicked); // // tableLayoutPanel4 // this.tableLayoutPanel4.AutoSize = true; this.tableLayoutPanel4.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel4.ColumnCount = 2; this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel4.Controls.Add(this.DownButton, 1, 1); this.tableLayoutPanel4.Controls.Add(this.UpButton, 0, 1); this.tableLayoutPanel4.Controls.Add(this.DeleteButton, 1, 0); this.tableLayoutPanel4.Controls.Add(this.AddButton, 0, 0); this.tableLayoutPanel4.Dock = System.Windows.Forms.DockStyle.Bottom; this.tableLayoutPanel4.Location = new System.Drawing.Point(0, 297); this.tableLayoutPanel4.Margin = new System.Windows.Forms.Padding(0, 5, 0, 0); this.tableLayoutPanel4.Name = "tableLayoutPanel4"; this.tableLayoutPanel4.RowCount = 2; this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.Size = new System.Drawing.Size(250, 68); this.tableLayoutPanel4.TabIndex = 8; // // DownButton // this.DownButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.DownButton.Location = new System.Drawing.Point(130, 34); this.DownButton.Margin = new System.Windows.Forms.Padding(0); this.DownButton.Name = "DownButton"; this.DownButton.Size = new System.Drawing.Size(120, 34); this.DownButton.TabIndex = 4; this.DownButton.Text = "Down"; this.DownButton.UseVisualStyleBackColor = true; this.DownButton.Click += new System.EventHandler(this.DownButton_Click); // // UpButton // this.UpButton.Location = new System.Drawing.Point(0, 34); this.UpButton.Margin = new System.Windows.Forms.Padding(0); this.UpButton.Name = "UpButton"; this.UpButton.Size = new System.Drawing.Size(120, 34); this.UpButton.TabIndex = 3; this.UpButton.Text = "Up"; this.UpButton.UseVisualStyleBackColor = true; this.UpButton.Click += new System.EventHandler(this.UpButton_Click); // // tableLayoutPanel5 // this.tableLayoutPanel5.AutoSize = true; this.tableLayoutPanel5.ColumnCount = 1; this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel5.Controls.Add(this.PictureQRcode, 0, 0); this.tableLayoutPanel5.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel5.Location = new System.Drawing.Point(638, 3); this.tableLayoutPanel5.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3); this.tableLayoutPanel5.Name = "tableLayoutPanel5"; this.tableLayoutPanel5.RowCount = 2; this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.Size = new System.Drawing.Size(268, 470); this.tableLayoutPanel5.TabIndex = 17; // // tableLayoutPanel3 // this.tableLayoutPanel3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); this.tableLayoutPanel3.AutoSize = true; this.tableLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel3.ColumnCount = 2; this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); this.tableLayoutPanel3.Location = new System.Drawing.Point(259, 479); this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); this.tableLayoutPanel3.Name = "tableLayoutPanel3"; this.tableLayoutPanel3.RowCount = 1; this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel3.Size = new System.Drawing.Size(367, 36); this.tableLayoutPanel3.TabIndex = 14; // // MyCancelButton // this.MyCancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.MyCancelButton.AutoSize = true; this.MyCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.MyCancelButton.Location = new System.Drawing.Point(212, 3); this.MyCancelButton.Margin = new System.Windows.Forms.Padding(3, 3, 0, 0); this.MyCancelButton.Name = "MyCancelButton"; this.MyCancelButton.Size = new System.Drawing.Size(155, 33); this.MyCancelButton.TabIndex = 39; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); // // OKButton // this.OKButton.AutoSize = true; this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK; this.OKButton.Dock = System.Windows.Forms.DockStyle.Left; this.OKButton.Location = new System.Drawing.Point(3, 3); this.OKButton.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(155, 33); this.OKButton.TabIndex = 38; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; this.OKButton.Click += new System.EventHandler(this.OKButton_Click); // // ConfigForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(796, 564); this.Controls.Add(this.tableLayoutPanel2); this.Controls.Add(this.panel2); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "ConfigForm"; this.Padding = new System.Windows.Forms.Padding(12, 13, 12, 13); this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Edit Servers"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ConfigForm_FormClosed); this.Shown += new System.EventHandler(this.ConfigForm_Shown); ((System.ComponentModel.ISupportInitialize)(this.PictureQRcode)).EndInit(); this.tableLayoutPanel2.ResumeLayout(false); this.tableLayoutPanel2.PerformLayout(); this.ServerGroupBox.ResumeLayout(false); this.ServerGroupBox.PerformLayout(); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumServerPort)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.NumUDPPort)).EndInit(); this.tableLayoutPanel7.ResumeLayout(false); this.tableLayoutPanel7.PerformLayout(); this.tableLayoutPanel4.ResumeLayout(false); this.tableLayoutPanel5.ResumeLayout(false); this.tableLayoutPanel5.PerformLayout(); this.tableLayoutPanel3.ResumeLayout(false); this.tableLayoutPanel3.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Button DeleteButton; private System.Windows.Forms.Button AddButton; private System.Windows.Forms.ListBox ServersListBox; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; private System.Windows.Forms.Button DownButton; private System.Windows.Forms.Button UpButton; private System.Windows.Forms.PictureBox PictureQRcode; private System.Windows.Forms.LinkLabel LinkUpdate; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel7; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel5; private System.Windows.Forms.GroupBox ServerGroupBox; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.ComboBox ObfsCombo; private System.Windows.Forms.Label labelObfs; private System.Windows.Forms.Label ServerPortLabel; private System.Windows.Forms.TextBox IPTextBox; private System.Windows.Forms.NumericUpDown NumServerPort; private System.Windows.Forms.TextBox PasswordTextBox; private System.Windows.Forms.Label EncryptionLabel; private System.Windows.Forms.ComboBox EncryptionSelect; private System.Windows.Forms.TextBox TextLink; private System.Windows.Forms.TextBox RemarksTextBox; private System.Windows.Forms.Label ObfsUDPLabel; private System.Windows.Forms.CheckBox CheckObfsUDP; private System.Windows.Forms.Label TCPProtocolLabel; private System.Windows.Forms.Label UDPoverTCPLabel; private System.Windows.Forms.CheckBox CheckUDPoverUDP; private System.Windows.Forms.Label LabelNote; private System.Windows.Forms.CheckBox PasswordLabel; private System.Windows.Forms.Label TCPoverUDPLabel; private System.Windows.Forms.CheckBox CheckTCPoverUDP; private System.Windows.Forms.ComboBox TCPProtocolComboBox; private System.Windows.Forms.Label labelObfsParam; private System.Windows.Forms.TextBox TextObfsParam; private System.Windows.Forms.Label labelGroup; private System.Windows.Forms.TextBox TextGroup; private System.Windows.Forms.CheckBox checkAdvSetting; private System.Windows.Forms.Label labelUDPPort; private System.Windows.Forms.NumericUpDown NumUDPPort; private System.Windows.Forms.CheckBox checkSSRLink; private System.Windows.Forms.Label labelRemarks; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; private System.Windows.Forms.Button MyCancelButton; private System.Windows.Forms.Button OKButton; private System.Windows.Forms.Label labelProtocolParam; private System.Windows.Forms.TextBox TextProtocolParam; private System.Windows.Forms.CheckBox IPLabel; } } ================================================ FILE: shadowsocks-csharp/View/ConfigForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Net; using Microsoft.Win32; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; using ZXing.QrCode.Internal; using Shadowsocks.Encryption; namespace Shadowsocks.View { public partial class ConfigForm : Form { private ShadowsocksController controller; private UpdateChecker updateChecker; // this is a copy of configuration that we are working on private Configuration _modifiedConfiguration; private int _oldSelectedIndex = -1; private bool _allowSave = true; private bool _ignoreLoad = false; private string _oldSelectedID = null; private string _SelectedID = null; public ConfigForm(ShadowsocksController controller, UpdateChecker updateChecker, int focusIndex) { this.Font = System.Drawing.SystemFonts.MessageBoxFont; InitializeComponent(); ServersListBox.Font = CreateFont(); NumServerPort.Minimum = IPEndPoint.MinPort; NumServerPort.Maximum = IPEndPoint.MaxPort; NumUDPPort.Minimum = IPEndPoint.MinPort; NumUDPPort.Maximum = IPEndPoint.MaxPort; this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.controller = controller; this.updateChecker = updateChecker; if (updateChecker.LatestVersionURL == null) LinkUpdate.Visible = false; foreach (string name in EncryptorFactory.GetEncryptor()) { EncryptorInfo info = EncryptorFactory.GetEncryptorInfo(name); if (info.display) EncryptionSelect.Items.Add(name); } UpdateTexts(); controller.ConfigChanged += controller_ConfigChanged; LoadCurrentConfiguration(); if (_modifiedConfiguration.index >= 0 && _modifiedConfiguration.index < _modifiedConfiguration.configs.Count) _oldSelectedID = _modifiedConfiguration.configs[_modifiedConfiguration.index].id; if (focusIndex == -1) { int index = _modifiedConfiguration.index + 1; if (index < 0 || index > _modifiedConfiguration.configs.Count) index = _modifiedConfiguration.configs.Count; focusIndex = index; } if (_modifiedConfiguration.isHideTips) PictureQRcode.Visible = false; int dpi_mul = Util.Utils.GetDpiMul(); //ServersListBox.Height = ServersListBox.Height * 4 / dpi_mul; ServersListBox.Width = ServersListBox.Width * dpi_mul / 4; //ServersListBox.Height = ServersListBox.Height * dpi_mul / 4; ServersListBox.Height = checkAdvSetting.Top + checkAdvSetting.Height; AddButton.Width = AddButton.Width * dpi_mul / 4; AddButton.Height = AddButton.Height * dpi_mul / 4; DeleteButton.Width = DeleteButton.Width * dpi_mul / 4; DeleteButton.Height = DeleteButton.Height * dpi_mul / 4; UpButton.Width = UpButton.Width * dpi_mul / 4; UpButton.Height = UpButton.Height * dpi_mul / 4; DownButton.Width = DownButton.Width * dpi_mul / 4; DownButton.Height = DownButton.Height * dpi_mul / 4; //IPTextBox.Width = IPTextBox.Width * dpi_mul / 4; //ServerPortNumericUpDown.Width = ServerPortNumericUpDown.Width * dpi_mul / 4; //PasswordTextBox.Width = PasswordTextBox.Width * dpi_mul / 4; //EncryptionSelect.Width = EncryptionSelect.Width * dpi_mul / 4; //TCPProtocolComboBox.Width = TCPProtocolComboBox.Width * dpi_mul / 4; //ObfsCombo.Width = ObfsCombo.Width * dpi_mul / 4; //TextObfsParam.Width = TextObfsParam.Width * dpi_mul / 4; //RemarksTextBox.Width = RemarksTextBox.Width * dpi_mul / 4; //TextGroup.Width = TextGroup.Width * dpi_mul / 4; //TextLink.Width = TextLink.Width * dpi_mul / 4; //TextUDPPort.Width = TextUDPPort.Width * dpi_mul / 4; //int font_height = 9; //EncryptionSelect.Height = EncryptionSelect.Height - font_height + font_height * dpi_mul / 4; //TCPProtocolComboBox.Height = TCPProtocolComboBox.Height - font_height + font_height * dpi_mul / 4; //ObfsCombo.Height = ObfsCombo.Height - font_height + font_height * dpi_mul / 4; //OKButton.Width = OKButton.Width * dpi_mul / 4; OKButton.Height = OKButton.Height * dpi_mul / 4; //MyCancelButton.Width = MyCancelButton.Width * dpi_mul / 4; MyCancelButton.Height = MyCancelButton.Height * dpi_mul / 4; DrawLogo(350 * dpi_mul / 4); //DrawLogo(350); ShowWindow(); if (focusIndex >= 0 && focusIndex < _modifiedConfiguration.configs.Count) { SetServerListSelectedIndex(focusIndex); LoadSelectedServer(); } UpdateServersListBoxTopIndex(); } private Font CreateFont() { try { return new System.Drawing.Font("Consolas", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); } catch { try { return new System.Drawing.Font("新宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); } catch { return new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); } } } private void UpdateTexts() { this.Text = I18N.GetString("Edit Servers") + "(" + (controller.GetCurrentConfiguration().shareOverLan ? "any" : "local") + ":" + controller.GetCurrentConfiguration().localPort.ToString() + I18N.GetString(" Version") + UpdateChecker.FullVersion + ")"; AddButton.Text = I18N.GetString("&Add"); DeleteButton.Text = I18N.GetString("&Delete"); UpButton.Text = I18N.GetString("Up"); DownButton.Text = I18N.GetString("Down"); const string mark_str = "* "; IPLabel.Text = mark_str + I18N.GetString("Server IP"); ServerPortLabel.Text = mark_str + I18N.GetString("Server Port"); labelUDPPort.Text = I18N.GetString("UDP Port"); PasswordLabel.Text = mark_str + I18N.GetString("Password"); EncryptionLabel.Text = mark_str + I18N.GetString("Encryption"); TCPProtocolLabel.Text = mark_str + I18N.GetString(TCPProtocolLabel.Text); labelObfs.Text = mark_str + I18N.GetString(labelObfs.Text); labelRemarks.Text = I18N.GetString("Remarks"); checkAdvSetting.Text = I18N.GetString(checkAdvSetting.Text); TCPoverUDPLabel.Text = I18N.GetString(TCPoverUDPLabel.Text); UDPoverTCPLabel.Text = I18N.GetString(UDPoverTCPLabel.Text); labelProtocolParam.Text = I18N.GetString(labelProtocolParam.Text); labelObfsParam.Text = I18N.GetString(labelObfsParam.Text); ObfsUDPLabel.Text = I18N.GetString(ObfsUDPLabel.Text); LabelNote.Text = I18N.GetString(LabelNote.Text); CheckTCPoverUDP.Text = I18N.GetString(CheckTCPoverUDP.Text); CheckUDPoverUDP.Text = I18N.GetString(CheckUDPoverUDP.Text); CheckObfsUDP.Text = I18N.GetString(CheckObfsUDP.Text); checkSSRLink.Text = I18N.GetString(checkSSRLink.Text); for (int i = 0; i < TCPProtocolComboBox.Items.Count; ++i) { TCPProtocolComboBox.Items[i] = I18N.GetString(TCPProtocolComboBox.Items[i].ToString()); } ServerGroupBox.Text = I18N.GetString("Server"); OKButton.Text = I18N.GetString("OK"); MyCancelButton.Text = I18N.GetString("Cancel"); LinkUpdate.MaximumSize = new Size(ServersListBox.Width, ServersListBox.Height); LinkUpdate.Text = String.Format(I18N.GetString("New version {0} {1} available"), UpdateChecker.Name, updateChecker.LatestVersionNumber); } private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); } private void ShowWindow() { this.Opacity = 1; this.Show(); IPTextBox.Focus(); } private int SaveOldSelectedServer() { try { if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count) { return 0; // no changes } Server server = new Server { server = IPTextBox.Text.Trim(), server_port = Convert.ToInt32(NumServerPort.Value), server_udp_port = Convert.ToInt32(NumUDPPort.Value), password = PasswordTextBox.Text, method = EncryptionSelect.Text, protocol = TCPProtocolComboBox.Text, protocolparam = TextProtocolParam.Text, obfs = ObfsCombo.Text, obfsparam = TextObfsParam.Text, remarks = RemarksTextBox.Text, group = TextGroup.Text.Trim(), udp_over_tcp = CheckUDPoverUDP.Checked, //obfs_udp = CheckObfsUDP.Checked, id = _SelectedID }; Configuration.CheckServer(server); int ret = 0; if (_modifiedConfiguration.configs[_oldSelectedIndex].server != server.server || _modifiedConfiguration.configs[_oldSelectedIndex].server_port != server.server_port || _modifiedConfiguration.configs[_oldSelectedIndex].remarks_base64 != server.remarks_base64 || _modifiedConfiguration.configs[_oldSelectedIndex].group != server.group ) { ret = 1; // display changed } Server oldServer = _modifiedConfiguration.configs[_oldSelectedIndex]; if (oldServer.isMatchServer(server)) { server.setObfsData(oldServer.getObfsData()); server.setProtocolData(oldServer.getProtocolData()); server.enable = oldServer.enable; } _modifiedConfiguration.configs[_oldSelectedIndex] = server; return ret; } catch (FormatException) { MessageBox.Show(I18N.GetString("Illegal port number format")); } catch (Exception ex) { MessageBox.Show(ex.Message); } return -1; // ERROR } private void DrawLogo(int width) { Bitmap drawArea = new Bitmap(width, width); using (Graphics g = Graphics.FromImage(drawArea)) { g.Clear(Color.White); Bitmap ngnl = Resources.ngnl; g.DrawImage(ngnl, new Rectangle(0, 0, width, width)); if (!_modifiedConfiguration.isHideTips) g.DrawString("Click the 'Link' text box", new Font("Arial", 14), new SolidBrush(Color.Black), new RectangleF(0, 0, 300, 300)); } PictureQRcode.Image = drawArea; } private void GenQR(string ssconfig) { int dpi_mul = Util.Utils.GetDpiMul(); int width = 350 * dpi_mul / 4; if (TextLink.Focused) { string qrText = ssconfig; QRCode code = ZXing.QrCode.Internal.Encoder.encode(qrText, ErrorCorrectionLevel.M); ByteMatrix m = code.Matrix; int blockSize = Math.Max(width / (m.Width + 2), 1); Bitmap drawArea = new Bitmap(((m.Width + 2) * blockSize), ((m.Height + 2) * blockSize)); using (Graphics g = Graphics.FromImage(drawArea)) { g.Clear(Color.White); using (Brush b = new SolidBrush(Color.Black)) { for (int row = 0; row < m.Width; row++) { for (int col = 0; col < m.Height; col++) { if (m[row, col] != 0) { g.FillRectangle(b, blockSize * (row + 1), blockSize * (col + 1), blockSize, blockSize); } } } } Bitmap ngnl = Resources.ngnl; int div = 13, div_l = 5, div_r = 8; int l = (m.Width * div_l + div - 1) / div * blockSize, r = (m.Width * div_r + div - 1) / div * blockSize; g.DrawImage(ngnl, new Rectangle(l + blockSize, l + blockSize, r - l, r - l)); } PictureQRcode.Image = drawArea; PictureQRcode.Visible = true; _modifiedConfiguration.isHideTips = true; } else { //PictureQRcode.Visible = false; DrawLogo(PictureQRcode.Width); } } private void LoadSelectedServer() { if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count) { Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex]; IPTextBox.Text = server.server; NumServerPort.Value = server.server_port; NumUDPPort.Value = server.server_udp_port; PasswordTextBox.Text = server.password; EncryptionSelect.Text = server.method ?? "aes-256-cfb"; if (string.IsNullOrEmpty(server.protocol)) { TCPProtocolComboBox.Text = "origin"; } else { TCPProtocolComboBox.Text = server.protocol ?? "origin"; } string obfs_text = server.obfs ?? "plain"; ObfsCombo.Text = obfs_text; TextProtocolParam.Text = server.protocolparam; TextObfsParam.Text = server.obfsparam; RemarksTextBox.Text = server.remarks; TextGroup.Text = server.group; CheckUDPoverUDP.Checked = server.udp_over_tcp; //CheckObfsUDP.Checked = server.obfs_udp; _SelectedID = server.id; ServerGroupBox.Visible = true; if (TCPProtocolComboBox.Text == "origin" && obfs_text == "plain" && !CheckUDPoverUDP.Checked ) { checkAdvSetting.Checked = false; } if (checkSSRLink.Checked) { TextLink.Text = server.GetSSRLinkForServer(); } else { TextLink.Text = server.GetSSLinkForServer(); } if (CheckTCPoverUDP.Checked || CheckUDPoverUDP.Checked || server.server_udp_port != 0) { checkAdvSetting.Checked = true; } //PasswordLabel.Checked = false; //IPLabel.Checked = false; Update_SSR_controls_Visable(); UpdateObfsTextbox(); TextLink.SelectAll(); GenQR(TextLink.Text); } else { ServerGroupBox.Visible = false; } } private void LoadConfiguration(Configuration configuration) { if (ServersListBox.Items.Count != _modifiedConfiguration.configs.Count) { ServersListBox.Items.Clear(); foreach (Server server in _modifiedConfiguration.configs) { if (!string.IsNullOrEmpty(server.group)) { ServersListBox.Items.Add(server.group + " - " + server.HiddenName()); } else { ServersListBox.Items.Add(" " + server.HiddenName()); } } } else { for (int i = 0; i < _modifiedConfiguration.configs.Count; ++i) { if (!string.IsNullOrEmpty(_modifiedConfiguration.configs[i].group)) { ServersListBox.Items[i] = _modifiedConfiguration.configs[i].group + " - " + _modifiedConfiguration.configs[i].HiddenName(); } else { ServersListBox.Items[i] = " " + _modifiedConfiguration.configs[i].HiddenName(); } } } } public void SetServerListSelectedIndex(int index) { ServersListBox.ClearSelected(); if (index < ServersListBox.Items.Count) ServersListBox.SelectedIndex = index; else _oldSelectedIndex = ServersListBox.SelectedIndex; } private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfiguration(); LoadConfiguration(_modifiedConfiguration); _allowSave = false; SetServerListSelectedIndex(_modifiedConfiguration.index); _allowSave = true; LoadSelectedServer(); } private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e) { if (_oldSelectedIndex == ServersListBox.SelectedIndex || ServersListBox.SelectedIndex == -1) { // we are moving back to oldSelectedIndex or doing a force move return; } if (_allowSave) { int change = SaveOldSelectedServer(); if (change == -1) { ServersListBox.SelectedIndex = _oldSelectedIndex; // go back return; } if (change == 1) { LoadConfiguration(_modifiedConfiguration); } } if (!_ignoreLoad) LoadSelectedServer(); _oldSelectedIndex = ServersListBox.SelectedIndex; } private void UpdateServersListBoxTopIndex(int style = 0) { int visibleItems = ServersListBox.ClientSize.Height / ServersListBox.ItemHeight; int index; if (style == 0) { index = ServersListBox.SelectedIndex; } else { var items = ServersListBox.SelectedIndices; index = (style == 1 ? items[0] : items[items.Count - 1]); } int topIndex = Math.Max(index - visibleItems / 2, 0); ServersListBox.TopIndex = topIndex; } private void AddButton_Click(object sender, EventArgs e) { if (SaveOldSelectedServer() == -1) { return; } Server server = _oldSelectedIndex >=0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count ? Configuration.CopyServer(_modifiedConfiguration.configs[_oldSelectedIndex]) : Configuration.GetDefaultServer(); _modifiedConfiguration.configs.Insert(_oldSelectedIndex < 0 ? 0 : _oldSelectedIndex + 1, server); LoadConfiguration(_modifiedConfiguration); _SelectedID = server.id; ServersListBox.SelectedIndex = _oldSelectedIndex + 1; _oldSelectedIndex = ServersListBox.SelectedIndex; } private void DeleteButton_Click(object sender, EventArgs e) { _oldSelectedIndex = ServersListBox.SelectedIndex; var items = ServersListBox.SelectedIndices; if (items.Count > 0) { int[] array = new int[items.Count]; int i = 0; foreach (int index in items) { array[i++] = index; } Array.Sort(array); for (--i; i >= 0; --i) { int index = array[i]; if (index >= 0 && index < _modifiedConfiguration.configs.Count) { _modifiedConfiguration.configs.RemoveAt(index); } } } if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count) { _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1; } if (_oldSelectedIndex < 0) { _oldSelectedIndex = 0; } if (_oldSelectedIndex < _modifiedConfiguration.configs.Count) ServersListBox.SelectedIndex = _oldSelectedIndex; LoadConfiguration(_modifiedConfiguration); SetServerListSelectedIndex(_oldSelectedIndex); LoadSelectedServer(); UpdateServersListBoxTopIndex(); } private void OKButton_Click(object sender, EventArgs e) { if (SaveOldSelectedServer() == -1) { return; } if (_modifiedConfiguration.configs.Count == 0) { MessageBox.Show(I18N.GetString("Please add at least one server")); return; } if (_oldSelectedID != null) { for (int i = 0; i < _modifiedConfiguration.configs.Count; ++i) { if (_modifiedConfiguration.configs[i].id == _oldSelectedID) { _modifiedConfiguration.index = i; break; } } } controller.SaveServersConfig(_modifiedConfiguration); this.Close(); } private void CancelButton_Click(object sender, EventArgs e) { this.Close(); } private void ConfigForm_Shown(object sender, EventArgs e) { IPTextBox.Focus(); } private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e) { controller.ConfigChanged -= controller_ConfigChanged; } private void UpButton_Click(object sender, EventArgs e) { _oldSelectedIndex = ServersListBox.SelectedIndex; int index = _oldSelectedIndex; SaveOldSelectedServer(); var items = ServersListBox.SelectedIndices; if (items.Count == 1) { if (index > 0 && index < _modifiedConfiguration.configs.Count) { _modifiedConfiguration.configs.Reverse(index - 1, 2); ServersListBox.ClearSelected(); ServersListBox.SelectedIndex = _oldSelectedIndex = index - 1; LoadConfiguration(_modifiedConfiguration); ServersListBox.ClearSelected(); ServersListBox.SelectedIndex = _oldSelectedIndex = index - 1; LoadSelectedServer(); } } else { List all_items = new List(); foreach (int item in items) { if (item == 0) return; all_items.Add(item); } foreach (int item in all_items) { _modifiedConfiguration.configs.Reverse(item - 1, 2); } _allowSave = false; _ignoreLoad = true; ServersListBox.SelectedIndex = _oldSelectedIndex = index - 1; LoadConfiguration(_modifiedConfiguration); ServersListBox.ClearSelected(); foreach (int item in all_items) { if (item != index) ServersListBox.SelectedIndex = _oldSelectedIndex = item - 1; } ServersListBox.SelectedIndex = _oldSelectedIndex = index - 1; _ignoreLoad = false; _allowSave = true; LoadSelectedServer(); } UpdateServersListBoxTopIndex(1); } private void DownButton_Click(object sender, EventArgs e) { _oldSelectedIndex = ServersListBox.SelectedIndex; int index = _oldSelectedIndex; SaveOldSelectedServer(); var items = ServersListBox.SelectedIndices; if (items.Count == 1) { if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count - 1) { _modifiedConfiguration.configs.Reverse(index, 2); ServersListBox.ClearSelected(); ServersListBox.SelectedIndex = _oldSelectedIndex = index + 1; LoadConfiguration(_modifiedConfiguration); ServersListBox.ClearSelected(); ServersListBox.SelectedIndex = _oldSelectedIndex = index + 1; LoadSelectedServer(); } } else { List rev_items = new List(); int max_index = ServersListBox.Items.Count - 1; foreach (int item in items) { if (item == max_index) return; rev_items.Insert(0, item); } foreach (int item in rev_items) { _modifiedConfiguration.configs.Reverse(item, 2); } _allowSave = false; _ignoreLoad = true; ServersListBox.SelectedIndex = _oldSelectedIndex = index + 1; LoadConfiguration(_modifiedConfiguration); ServersListBox.ClearSelected(); foreach (int item in rev_items) { if (item != index) ServersListBox.SelectedIndex = _oldSelectedIndex = item + 1; } ServersListBox.SelectedIndex = _oldSelectedIndex = index + 1; _ignoreLoad = false; _allowSave = true; LoadSelectedServer(); } UpdateServersListBoxTopIndex(2); } private void TextBox_Enter(object sender, EventArgs e) { int change = SaveOldSelectedServer(); if (change == 1) { LoadConfiguration(_modifiedConfiguration); } LoadSelectedServer(); ((TextBox)sender).SelectAll(); } private void TextBox_MouseUp(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { ((TextBox)sender).SelectAll(); } } private void LinkUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(updateChecker.LatestVersionURL); } private void PasswordLabel_CheckedChanged(object sender, EventArgs e) { if (PasswordLabel.Checked) { PasswordTextBox.UseSystemPasswordChar = false; } else { PasswordTextBox.UseSystemPasswordChar = true; } } private void UpdateObfsTextbox() { try { Obfs.ObfsBase obfs = (Obfs.ObfsBase)Obfs.ObfsFactory.GetObfs(ObfsCombo.Text); int[] properties = obfs.GetObfs()[ObfsCombo.Text]; if (properties[2] > 0) { TextObfsParam.Enabled = true; } else { TextObfsParam.Enabled = false; } } catch { TextObfsParam.Enabled = true; } } private void ObfsCombo_TextChanged(object sender, EventArgs e) { UpdateObfsTextbox(); } private void checkSSRLink_CheckedChanged(object sender, EventArgs e) { int change = SaveOldSelectedServer(); if (change == 1) { LoadConfiguration(_modifiedConfiguration); } LoadSelectedServer(); } private void checkAdvSetting_CheckedChanged(object sender, EventArgs e) { Update_SSR_controls_Visable(); } private void Update_SSR_controls_Visable() { SuspendLayout(); if (checkAdvSetting.Checked) { labelUDPPort.Visible = true; NumUDPPort.Visible = true; //TCPoverUDPLabel.Visible = true; //CheckTCPoverUDP.Visible = true; } else { labelUDPPort.Visible = false; NumUDPPort.Visible = false; //TCPoverUDPLabel.Visible = false; //CheckTCPoverUDP.Visible = false; } if (checkAdvSetting.Checked) { UDPoverTCPLabel.Visible = true; CheckUDPoverUDP.Visible = true; } else { UDPoverTCPLabel.Visible = false; CheckUDPoverUDP.Visible = false; } ResumeLayout(); } private void IPLabel_CheckedChanged(object sender, EventArgs e) { if (IPLabel.Checked) { IPTextBox.UseSystemPasswordChar = false; } else { IPTextBox.UseSystemPasswordChar = true; } } } } ================================================ FILE: shadowsocks-csharp/View/ConfigForm.resx ================================================ text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/InputPassword.Designer.cs ================================================ namespace Shadowsocks.View { partial class InputPassword { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.textPassword = new System.Windows.Forms.TextBox(); this.buttonOK = new System.Windows.Forms.Button(); this.label_info = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textPassword // this.textPassword.ImeMode = System.Windows.Forms.ImeMode.Off; this.textPassword.Location = new System.Drawing.Point(41, 44); this.textPassword.Name = "textPassword"; this.textPassword.Size = new System.Drawing.Size(330, 21); this.textPassword.TabIndex = 0; this.textPassword.UseSystemPasswordChar = true; // // buttonOK // this.buttonOK.Location = new System.Drawing.Point(131, 76); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(151, 28); this.buttonOK.TabIndex = 1; this.buttonOK.Text = "OK"; this.buttonOK.UseVisualStyleBackColor = true; this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click); // // label_info // this.label_info.AutoSize = true; this.label_info.Location = new System.Drawing.Point(24, 26); this.label_info.Name = "label_info"; this.label_info.Size = new System.Drawing.Size(350, 15); this.label_info.TabIndex = 2; this.label_info.Text = "Parse gui-config.json error, maybe require password to decrypt"; // // InputPassword // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.ClientSize = new System.Drawing.Size(412, 119); this.Controls.Add(this.label_info); this.Controls.Add(this.buttonOK); this.Controls.Add(this.textPassword); this.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.ImeMode = System.Windows.Forms.ImeMode.Off; this.KeyPreview = true; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "InputPassword"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "InputPassword"; this.TopMost = true; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.InputPassword_KeyDown); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textPassword; private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.Label label_info; } } ================================================ FILE: shadowsocks-csharp/View/InputPassword.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Properties; using Shadowsocks.Controller; namespace Shadowsocks.View { public partial class InputPassword : Form { public string password; public InputPassword() { InitializeComponent(); this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.Text = I18N.GetString("InputPassword"); label_info.Text = I18N.GetString(label_info.Text); } private void buttonOK_Click(object sender, EventArgs e) { password = textPassword.Text; this.DialogResult = DialogResult.OK; Close(); } private void InputPassword_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { password = textPassword.Text; this.DialogResult = DialogResult.OK; Close(); } } } } ================================================ FILE: shadowsocks-csharp/View/InputPassword.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/LogForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class LogForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.logMenu = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.clearLogToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.showInExplorerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.closeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.fontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.wrapTextToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.alwaysOnTopToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.logTextBox = new System.Windows.Forms.TextBox(); this.refreshTimer = new System.Windows.Forms.Timer(this.components); this.logMenu.SuspendLayout(); this.SuspendLayout(); // // logMenu // this.logMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.viewToolStripMenuItem}); this.logMenu.Location = new System.Drawing.Point(0, 0); this.logMenu.Name = "logMenu"; this.logMenu.Size = new System.Drawing.Size(986, 25); this.logMenu.TabIndex = 0; this.logMenu.Text = "menuStrip1"; // // fileToolStripMenuItem // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.clearLogToolStripMenuItem, this.showInExplorerToolStripMenuItem, this.toolStripSeparator1, this.closeToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(39, 21); this.fileToolStripMenuItem.Text = "&File"; // // clearLogToolStripMenuItem // this.clearLogToolStripMenuItem.Name = "clearLogToolStripMenuItem"; this.clearLogToolStripMenuItem.Size = new System.Drawing.Size(174, 22); this.clearLogToolStripMenuItem.Text = "Clear &log"; this.clearLogToolStripMenuItem.Click += new System.EventHandler(this.clearLogToolStripMenuItem_Click); // // showInExplorerToolStripMenuItem // this.showInExplorerToolStripMenuItem.Name = "showInExplorerToolStripMenuItem"; this.showInExplorerToolStripMenuItem.Size = new System.Drawing.Size(174, 22); this.showInExplorerToolStripMenuItem.Text = "Show in &Explorer"; this.showInExplorerToolStripMenuItem.Click += new System.EventHandler(this.showInExplorerToolStripMenuItem_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Size = new System.Drawing.Size(171, 6); // // closeToolStripMenuItem // this.closeToolStripMenuItem.Name = "closeToolStripMenuItem"; this.closeToolStripMenuItem.Size = new System.Drawing.Size(174, 22); this.closeToolStripMenuItem.Text = "&Close"; this.closeToolStripMenuItem.Click += new System.EventHandler(this.closeToolStripMenuItem_Click); // // viewToolStripMenuItem // this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fontToolStripMenuItem, this.wrapTextToolStripMenuItem, this.alwaysOnTopToolStripMenuItem}); this.viewToolStripMenuItem.Name = "viewToolStripMenuItem"; this.viewToolStripMenuItem.Size = new System.Drawing.Size(47, 21); this.viewToolStripMenuItem.Text = "&View"; // // fontToolStripMenuItem // this.fontToolStripMenuItem.Name = "fontToolStripMenuItem"; this.fontToolStripMenuItem.Size = new System.Drawing.Size(158, 22); this.fontToolStripMenuItem.Text = "&Font"; this.fontToolStripMenuItem.Click += new System.EventHandler(this.fontToolStripMenuItem_Click); // // wrapTextToolStripMenuItem // this.wrapTextToolStripMenuItem.Name = "wrapTextToolStripMenuItem"; this.wrapTextToolStripMenuItem.Size = new System.Drawing.Size(158, 22); this.wrapTextToolStripMenuItem.Text = "&Wrap text"; this.wrapTextToolStripMenuItem.CheckedChanged += new System.EventHandler(this.wrapTextToolStripMenuItem_CheckedChanged); this.wrapTextToolStripMenuItem.Click += new System.EventHandler(this.wrapTextToolStripMenuItem_Click); // // alwaysOnTopToolStripMenuItem // this.alwaysOnTopToolStripMenuItem.Name = "alwaysOnTopToolStripMenuItem"; this.alwaysOnTopToolStripMenuItem.Size = new System.Drawing.Size(158, 22); this.alwaysOnTopToolStripMenuItem.Text = "&Always on top"; this.alwaysOnTopToolStripMenuItem.CheckedChanged += new System.EventHandler(this.alwaysOnTopToolStripMenuItem_CheckedChanged); this.alwaysOnTopToolStripMenuItem.Click += new System.EventHandler(this.alwaysOnTopToolStripMenuItem_Click); // // logTextBox // this.logTextBox.BackColor = System.Drawing.Color.Black; this.logTextBox.Dock = System.Windows.Forms.DockStyle.Fill; this.logTextBox.Font = new System.Drawing.Font("Courier New", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.logTextBox.ForeColor = System.Drawing.Color.White; this.logTextBox.Location = new System.Drawing.Point(0, 25); this.logTextBox.MaxLength = 2147483647; this.logTextBox.Multiline = true; this.logTextBox.Name = "logTextBox"; this.logTextBox.ReadOnly = true; this.logTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.logTextBox.Size = new System.Drawing.Size(986, 411); this.logTextBox.TabIndex = 1; this.logTextBox.WordWrap = false; // // refreshTimer // this.refreshTimer.Enabled = true; this.refreshTimer.Tick += new System.EventHandler(this.refreshTimer_Tick); // // LogForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(986, 436); this.Controls.Add(this.logTextBox); this.Controls.Add(this.logMenu); this.Name = "LogForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Log Viewer"; this.Load += new System.EventHandler(this.LogForm_Load); this.Shown += new System.EventHandler(this.LogForm_Shown); this.logMenu.ResumeLayout(false); this.logMenu.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip logMenu; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem showInExplorerToolStripMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripMenuItem closeToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem viewToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem fontToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem wrapTextToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem alwaysOnTopToolStripMenuItem; private System.Windows.Forms.TextBox logTextBox; private System.Windows.Forms.ToolStripMenuItem clearLogToolStripMenuItem; private System.Windows.Forms.Timer refreshTimer; } } ================================================ FILE: shadowsocks-csharp/View/LogForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; namespace Shadowsocks.View { public partial class LogForm : Form { private readonly ShadowsocksController _controller; private const int MaxReadSize = 65536; private string _currentLogFile; private string _currentLogFileName; private long _currentOffset; public LogForm(ShadowsocksController controller) { _controller = controller; InitializeComponent(); Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); UpdateTexts(); } private void UpdateTexts() { fileToolStripMenuItem.Text = I18N.GetString("&File"); clearLogToolStripMenuItem.Text = I18N.GetString("Clear &log"); showInExplorerToolStripMenuItem.Text = I18N.GetString("Show in &Explorer"); closeToolStripMenuItem.Text = I18N.GetString("&Close"); viewToolStripMenuItem.Text = I18N.GetString("&View"); fontToolStripMenuItem.Text = I18N.GetString("&Font..."); wrapTextToolStripMenuItem.Text = I18N.GetString("&Wrap Text"); alwaysOnTopToolStripMenuItem.Text = I18N.GetString("&Always on top"); Text = I18N.GetString("Log Viewer"); } private void closeToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } private void showInExplorerToolStripMenuItem_Click(object sender, EventArgs _) { try { string argument = "/n" + ",/select," + Logging.LogFile; System.Diagnostics.Process.Start("explorer.exe", argument); } catch (Exception e) { Logging.LogUsefulException(e); } } private void LogForm_Load(object sender, EventArgs e) { ReadLog(); } private void ReadLog() { var newLogFile = Logging.LogFile; if (newLogFile != _currentLogFile) { _currentOffset = 0; _currentLogFile = newLogFile; _currentLogFileName = Logging.LogFileName; } try { using ( var reader = new StreamReader(new FileStream(newLogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) ) { if (_currentOffset == 0) { var maxSize = reader.BaseStream.Length; if (maxSize > MaxReadSize) { reader.BaseStream.Seek(-MaxReadSize, SeekOrigin.End); reader.ReadLine(); } } else { reader.BaseStream.Seek(_currentOffset, SeekOrigin.Begin); } var txt = reader.ReadToEnd(); if (!string.IsNullOrEmpty(txt)) { logTextBox.AppendText(txt); logTextBox.ScrollToCaret(); } _currentOffset = reader.BaseStream.Position; } } catch (FileNotFoundException) { } Text = $@"{I18N.GetString("Log Viewer")} {_currentLogFileName}"; } private void refreshTimer_Tick(object sender, EventArgs e) { ReadLog(); } private void LogForm_Shown(object sender, EventArgs e) { logTextBox.ScrollToCaret(); } private void fontToolStripMenuItem_Click(object sender, EventArgs e) { using (FontDialog fontDialog = new FontDialog()) { fontDialog.Font = logTextBox.Font; if (fontDialog.ShowDialog() == DialogResult.OK) { logTextBox.Font = fontDialog.Font; } } } private void wrapTextToolStripMenuItem_Click(object sender, EventArgs e) { wrapTextToolStripMenuItem.Checked = !wrapTextToolStripMenuItem.Checked; } private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e) { alwaysOnTopToolStripMenuItem.Checked = !alwaysOnTopToolStripMenuItem.Checked; } private void wrapTextToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { logTextBox.WordWrap = wrapTextToolStripMenuItem.Checked; logTextBox.ScrollToCaret(); } private void alwaysOnTopToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { TopMost = alwaysOnTopToolStripMenuItem.Checked; } private void clearLogToolStripMenuItem_Click(object sender, EventArgs e) { Logging.Clear(); _currentOffset = 0; logTextBox.Clear(); } } } ================================================ FILE: shadowsocks-csharp/View/LogForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 17, 17 True 122, 17 ================================================ FILE: shadowsocks-csharp/View/MenuViewController.cs ================================================ using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using ZXing; using ZXing.Common; using ZXing.QrCode; using System.Threading; using System.Text.RegularExpressions; using Shadowsocks.Util; using System.Net.Sockets; using System.Net; using System.Linq; namespace Shadowsocks.View { public class EventParams { public object _sender; public EventArgs _e; public EventParams(object sender, EventArgs e) { _sender = sender; _e = e; } } public class MenuViewController { // 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; private UpdateFreeNode updateFreeNodeChecker; private UpdateSubscribeManager updateSubscribeManager; private bool isBeingUsed = false; private NotifyIcon _notifyIcon; private ContextMenu contextMenu1; private MenuItem noModifyItem; private MenuItem enableItem; private MenuItem PACModeItem; private MenuItem globalModeItem; private MenuItem modeItem; private MenuItem ruleBypassLan; private MenuItem ruleBypassChina; private MenuItem ruleBypassNotChina; private MenuItem ruleUser; private MenuItem ruleDisableBypass; private MenuItem SeperatorItem; private MenuItem ServersItem; //private MenuItem SelectRandomItem; private MenuItem sameHostForSameTargetItem; private MenuItem UpdateItem; private ConfigForm configForm; private SettingsForm settingsForm; private ServerLogForm serverLogForm; private PortSettingsForm portMapForm; private SubscribeForm subScribeForm; private LogForm logForm; private string _urlToOpen; private System.Timers.Timer timerDetectVirus; private System.Timers.Timer timerDelayCheckUpdate; private System.Timers.Timer timerUpdateLatency; private bool configfrom_open = false; private List eventList = new List(); public MenuViewController(ShadowsocksController controller) { _controller = controller; LoadMenu(); _controller.ToggleModeChanged += controller_ToggleModeChanged; _controller.ToggleRuleModeChanged += controller_ToggleRuleModeChanged; _controller.ConfigChanged += controller_ConfigChanged; _controller.PACFileReadyToOpen += controller_FileReadyToOpen; _controller.UserRuleFileReadyToOpen += controller_FileReadyToOpen; _controller.Errored += controller_Errored; _controller.UpdatePACFromGFWListCompleted += controller_UpdatePACFromGFWListCompleted; _controller.UpdatePACFromGFWListError += controller_UpdatePACFromGFWListError; _controller.ShowConfigFormEvent += Config_Click; _notifyIcon = new NotifyIcon(); UpdateTrayIcon(); _notifyIcon.Visible = true; _notifyIcon.ContextMenu = contextMenu1; _notifyIcon.MouseClick += notifyIcon1_Click; //_notifyIcon.MouseDoubleClick += notifyIcon1_DoubleClick; updateChecker = new UpdateChecker(); updateChecker.NewVersionFound += updateChecker_NewVersionFound; updateFreeNodeChecker = new UpdateFreeNode(); updateFreeNodeChecker.NewFreeNodeFound += updateFreeNodeChecker_NewFreeNodeFound; updateSubscribeManager = new UpdateSubscribeManager(); LoadCurrentConfiguration(); timerDetectVirus = new System.Timers.Timer(1000.0 * 30); timerDetectVirus.Elapsed += timerDetectVirus_Elapsed; timerDetectVirus.Start(); //this interval will change timerDelayCheckUpdate = new System.Timers.Timer(1000.0 * 10); timerDelayCheckUpdate.Elapsed += timerDelayCheckUpdate_Elapsed; timerDelayCheckUpdate.Start(); timerUpdateLatency = new System.Timers.Timer(1000.0 * 3); timerUpdateLatency.Elapsed += timerUpdateLatency_Elapsed; timerUpdateLatency.Start(); } private void timerDetectVirus_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (Utils.isVirusExist()) { Quit(); } } private void timerDelayCheckUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (timerDelayCheckUpdate != null) { if (timerDelayCheckUpdate.Interval <= 1000.0 * 30) { timerDelayCheckUpdate.Interval = 1000.0 * 60 * 5; } else { timerDelayCheckUpdate.Interval = 1000.0 * 60 * 60 * 2; } } updateChecker.CheckUpdate(_controller.GetCurrentConfiguration()); Configuration cfg = _controller.GetCurrentConfiguration(); if (cfg.isDefaultConfig() || cfg.nodeFeedAutoUpdate) { updateSubscribeManager.CreateTask(_controller.GetCurrentConfiguration(), updateFreeNodeChecker, -1, !cfg.isDefaultConfig(), false); } } private void timerUpdateLatency_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { timerUpdateLatency.Interval = 1000.0 * 60 * 30; timerUpdateLatency.Stop(); try { Configuration configuration = _controller.GetCurrentConfiguration(); for (int i = 0; i < configuration.configs.Count; i++) { var server = configuration.configs[i]; server.tcpingLatency(); Utils.ReleaseMemory(); } } catch { timerUpdateLatency.Interval = 1000.0 * 60; } UpdateServersMenu(); timerUpdateLatency.Start(); } void controller_Errored(object sender, System.IO.ErrorEventArgs e) { MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message)); } private void UpdateTrayIcon() { int dpi; using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { dpi = (int)graphics.DpiX; } Configuration config = _controller.GetCurrentConfiguration(); bool enabled = config.sysProxyMode != (int)ProxyMode.NoModify && config.sysProxyMode != (int)ProxyMode.Direct; string server; if (config.random) { server = config.balanceAlgorithm; } else { int server_current = config.index; server = config.configs[server_current].remarks; } bool global = config.sysProxyMode == (int)ProxyMode.Global; bool random = config.random; try { using (Bitmap icon = new Bitmap("icon.png")) { _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon()); } } catch { Bitmap icon = null; if (dpi < 97) { // dpi = 96; icon = Resources.ss16; } else if (dpi < 121) { // dpi = 120; icon = Resources.ss20; } else { icon = Resources.ss24; } double mul_a = 1.0, mul_r = 1.0, mul_g = 1.0, mul_b = 1.0; if (!enabled) { mul_g = 0.4; } else if (!global) { mul_b = 0.4; mul_g = 0.8; } if (!random) { mul_r = 0.4; } using (Bitmap iconCopy = new Bitmap(icon)) { for (int x = 0; x < iconCopy.Width; x++) { for (int y = 0; y < iconCopy.Height; y++) { Color color = icon.GetPixel(x, y); iconCopy.SetPixel(x, y, Color.FromArgb((byte)(color.A * mul_a), ((byte)(color.R * mul_r)), ((byte)(color.G * mul_g)), ((byte)(color.B * mul_b)))); } } _notifyIcon.Icon = Icon.FromHandle(iconCopy.GetHicon()); } } // we want to show more details but notify icon title is limited to 63 characters string text = (enabled ? (global ? I18N.GetString("Global") : I18N.GetString("PAC")) : I18N.GetString("Disable system proxy")) + "\r\n" + server + "\r\n" + String.Format(I18N.GetString("Running: Port {0}"), config.localPort) // this feedback is very important because they need to know Shadowsocks is running ; _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); } private void LoadMenu() { contextMenu1 = new ContextMenu(new MenuItem[] { modeItem = CreateMenuGroup("Mode", new MenuItem[] { enableItem = CreateMenuItem("Disable system proxy", new EventHandler(EnableItem_Click)), PACModeItem = CreateMenuItem("PAC", new EventHandler(PACModeItem_Click)), globalModeItem = CreateMenuItem("Global", new EventHandler(GlobalModeItem_Click)), new MenuItem("-"), noModifyItem = CreateMenuItem("No modify system proxy", new EventHandler(NoModifyItem_Click)) }), CreateMenuGroup("PAC ", new MenuItem[] { CreateMenuItem("Update local PAC from GFWList", new EventHandler(UpdatePACFromGFWListItem_Click)), new MenuItem("-"), CreateMenuItem("Update local PAC from Lan IP list", new EventHandler(UpdatePACFromLanIPListItem_Click)), CreateMenuItem("Update local PAC from Chn White list", new EventHandler(UpdatePACFromCNWhiteListItem_Click)), CreateMenuItem("Update local PAC from Chn IP list", new EventHandler(UpdatePACFromCNIPListItem_Click)), new MenuItem("-"), CreateMenuItem("Update local PAC from Chn Only list", new EventHandler(UpdatePACFromCNOnlyListItem_Click)), new MenuItem("-"), CreateMenuItem("Copy PAC URL", new EventHandler(CopyPACURLItem_Click)), CreateMenuItem("Edit local PAC file...", new EventHandler(EditPACFileItem_Click)), CreateMenuItem("Edit user rule for GFWList...", new EventHandler(EditUserRuleFileForGFWListItem_Click)), }), CreateMenuGroup("Proxy rule", new MenuItem[] { ruleBypassLan = CreateMenuItem("Bypass LAN", new EventHandler(RuleBypassLanItem_Click)), ruleBypassChina = CreateMenuItem("Bypass LAN && China", new EventHandler(RuleBypassChinaItem_Click)), ruleBypassNotChina = CreateMenuItem("Bypass LAN && not China", new EventHandler(RuleBypassNotChinaItem_Click)), ruleUser = CreateMenuItem("User custom", new EventHandler(RuleUserItem_Click)), new MenuItem("-"), ruleDisableBypass = CreateMenuItem("Disable bypass", new EventHandler(RuleBypassDisableItem_Click)), }), new MenuItem("-"), ServersItem = CreateMenuGroup("Servers", new MenuItem[] { SeperatorItem = new MenuItem("-"), CreateMenuItem("Edit servers...", new EventHandler(Config_Click)), new MenuItem("-"), CreateMenuItem("Import from file...", new EventHandler(Import_Click)), CreateMenuItem("Import from clipboard SSR links...", new EventHandler(CopyAddress_Click)), CreateMenuItem("Import from screen QRCode...", new EventHandler(ScanQRCodeItem_Click)), new MenuItem("-"), sameHostForSameTargetItem = CreateMenuItem("Same host for same address", new EventHandler(SelectSameHostForSameTargetItem_Click)), new MenuItem("-"), CreateMenuItem("Server statistic...", new EventHandler(ShowServerLogItem_Click)), CreateMenuItem("Disconnect current", new EventHandler(DisconnectCurrent_Click)), }), CreateMenuGroup("Servers Subscribe", new MenuItem[] { CreateMenuItem("Subscribe setting...", new EventHandler(SubscribeSetting_Click)), CreateMenuItem("Update subscribe SSR node", new EventHandler(CheckNodeUpdate_Click)), CreateMenuItem("Update subscribe SSR node(use proxy)", new EventHandler(CheckNodeUpdateUseProxy_Click)), }), CreateMenuItem("Global settings...", new EventHandler(Setting_Click)), CreateMenuItem("Port settings...", new EventHandler(ShowPortMapItem_Click)), UpdateItem = CreateMenuItem("Update available", new EventHandler(UpdateItem_Clicked)), new MenuItem("-"), CreateMenuItem("Reset password...", new EventHandler(ResetPasswordItem_Click)), CreateMenuGroup("Help", new MenuItem[] { CreateMenuItem("Check update", new EventHandler(CheckUpdate_Click)), CreateMenuItem("Show logs...", new EventHandler(ShowLogItem_Click)), CreateMenuItem("Open wiki...", new EventHandler(OpenWiki_Click)), CreateMenuItem("Feedback...", new EventHandler(FeedbackItem_Click)), new MenuItem("-"), CreateMenuItem("Gen custom QRCode...", new EventHandler(showURLFromQRCode)), new MenuItem("-"), CreateMenuItem("About...", new EventHandler(AboutItem_Click)), CreateMenuItem("Donate...", new EventHandler(DonateItem_Click)), }), CreateMenuItem("Quit", new EventHandler(Quit_Click)) }); UpdateItem.Visible = false; } private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); UpdateTrayIcon(); } private void controller_ToggleModeChanged(object sender, EventArgs e) { Configuration config = _controller.GetCurrentConfiguration(); UpdateSysProxyMode(config); } private void controller_ToggleRuleModeChanged(object sender, EventArgs e) { Configuration config = _controller.GetCurrentConfiguration(); UpdateProxyRule(config); } void controller_FileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e) { string argument = @"/select, " + e.Path; System.Diagnostics.Process.Start("explorer.exe", argument); } void ShowBalloonTip(string title, string content, ToolTipIcon icon, int timeout) { _notifyIcon.BalloonTipTitle = title; _notifyIcon.BalloonTipText = content; _notifyIcon.BalloonTipIcon = icon; _notifyIcon.ShowBalloonTip(timeout); } void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e) { GFWListUpdater updater = (GFWListUpdater)sender; ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000); Logging.LogUsefulException(e.GetException()); } void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e) { GFWListUpdater updater = (GFWListUpdater)sender; string result = e.Success ? (updater.update_type <= 1 ? I18N.GetString("PAC updated") : I18N.GetString("Domain white list list updated")) : I18N.GetString("No updates found. Please report to GFWList if you have problems with it."); ShowBalloonTip(I18N.GetString("Shadowsocks"), result, ToolTipIcon.Info, 1000); } void updateFreeNodeChecker_NewFreeNodeFound(object sender, EventArgs e) { if (configfrom_open) { eventList.Add(new EventParams(sender, e)); return; } string lastGroup = null; int count = 0; if (!String.IsNullOrEmpty(updateFreeNodeChecker.FreeNodeResult)) { List urls = new List(); updateFreeNodeChecker.FreeNodeResult = updateFreeNodeChecker.FreeNodeResult.TrimEnd('\r', '\n', ' '); Configuration config = _controller.GetCurrentConfiguration(); Server selected_server = null; if (config.index >= 0 && config.index < config.configs.Count) { selected_server = config.configs[config.index]; } try { updateFreeNodeChecker.FreeNodeResult = Util.Base64.DecodeBase64(updateFreeNodeChecker.FreeNodeResult); } catch { updateFreeNodeChecker.FreeNodeResult = ""; } int max_node_num = 0; Match match_maxnum = Regex.Match(updateFreeNodeChecker.FreeNodeResult, "^MAX=([0-9]+)"); if (match_maxnum.Success) { try { max_node_num = Convert.ToInt32(match_maxnum.Groups[1].Value, 10); } catch { } } URL_Split(updateFreeNodeChecker.FreeNodeResult, ref urls); for (int i = urls.Count - 1; i >= 0; --i) { if (!urls[i].StartsWith("ssr")) urls.RemoveAt(i); } if (urls.Count > 0) { bool keep_selected_server = false; // set 'false' if import all nodes if (max_node_num <= 0 || max_node_num >= urls.Count) { urls.Reverse(); } else { Random r = new Random(); Util.Utils.Shuffle(urls, r); urls.RemoveRange(max_node_num, urls.Count - max_node_num); if (!config.isDefaultConfig()) keep_selected_server = true; } string curGroup = null; foreach (string url in urls) { try // try get group name { Server server = new Server(url, null); if (!String.IsNullOrEmpty(server.group)) { curGroup = server.group; break; } } catch { } } string subscribeURL = updateSubscribeManager.URL; if (String.IsNullOrEmpty(curGroup)) { curGroup = subscribeURL; } for (int i = 0; i < config.serverSubscribes.Count; ++i) { if (subscribeURL == config.serverSubscribes[i].URL) { lastGroup = config.serverSubscribes[i].Group; config.serverSubscribes[i].Group = curGroup; break; } } if (String.IsNullOrEmpty(lastGroup)) { lastGroup = curGroup; } if (keep_selected_server && selected_server.group == curGroup) { bool match = false; for (int i = 0; i < urls.Count; ++i) { try { Server server = new Server(urls[i], null); if (selected_server.isMatchServer(server)) { match = true; break; } } catch { } } if (!match) { urls.RemoveAt(0); urls.Add(selected_server.GetSSRLinkForServer()); } } // import all, find difference { Dictionary old_servers = new Dictionary(); Dictionary old_insert_servers = new Dictionary(); if (!String.IsNullOrEmpty(lastGroup)) { for (int i = config.configs.Count - 1; i >= 0; --i) { if (lastGroup == config.configs[i].group) { old_servers[config.configs[i].id] = config.configs[i]; } } } foreach (string url in urls) { try { Server server = new Server(url, curGroup); bool match = false; if (!match) { foreach (KeyValuePair pair in old_insert_servers) { if (server.isMatchServer(pair.Value)) { match = true; break; } } } old_insert_servers[server.id] = server; if (!match) { foreach (KeyValuePair pair in old_servers) { if (server.isMatchServer(pair.Value)) { match = true; old_servers.Remove(pair.Key); pair.Value.CopyServerInfo(server); ++count; break; } } } if (!match) { int insert_index = config.configs.Count; for (int index = config.configs.Count - 1; index >= 0; --index) { if (config.configs[index].group == curGroup) { insert_index = index + 1; break; } } config.configs.Insert(insert_index, server); ++count; } } catch { } } foreach (KeyValuePair pair in old_servers) { for (int i = config.configs.Count - 1; i >= 0; --i) { if (config.configs[i].id == pair.Key) { config.configs.RemoveAt(i); break; } } } _controller.SaveServersConfig(config); } config = _controller.GetCurrentConfiguration(); if (selected_server != null) { bool match = false; for (int i = config.configs.Count - 1; i >= 0; --i) { if (config.configs[i].id == selected_server.id) { config.index = i; match = true; break; } else if (config.configs[i].group == selected_server.group) { if (config.configs[i].isMatchServer(selected_server)) { config.index = i; match = true; break; } } } if (!match) { config.index = config.configs.Count - 1; } } else { config.index = config.configs.Count - 1; } if (count > 0) { for (int i = 0; i < config.serverSubscribes.Count; ++i) { if (config.serverSubscribes[i].URL == updateFreeNodeChecker.subscribeTask.URL) { config.serverSubscribes[i].LastUpdateTime = (UInt64)Math.Floor(DateTime.Now.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); } } } _controller.SaveServersConfig(config); } } if (count > 0) { if (updateFreeNodeChecker.noitify) ShowBalloonTip(I18N.GetString("Success"), string.Format(I18N.GetString("Update subscribe {0} success"), lastGroup), ToolTipIcon.Info, 10000); } else { if (lastGroup == null) { lastGroup = updateFreeNodeChecker.subscribeTask.Group; //lastGroup = updateSubscribeManager.LastGroup; } ShowBalloonTip(I18N.GetString("Error"), String.Format(I18N.GetString("Update subscribe {0} failure"), lastGroup), ToolTipIcon.Info, 10000); } if (updateSubscribeManager.Next()) { } } void updateChecker_NewVersionFound(object sender, EventArgs e) { if (string.IsNullOrEmpty(updateChecker.LatestVersionNumber)) { Logging.Log(LogLevel.Error, "connect to update server error"); } else { if (!UpdateItem.Visible) { ShowBalloonTip(String.Format(I18N.GetString("{0} {1} Update Found"), UpdateChecker.Name, updateChecker.LatestVersionNumber), I18N.GetString("Click menu to download"), ToolTipIcon.Info, 10000); _notifyIcon.BalloonTipClicked += notifyIcon1_BalloonTipClicked; timerDelayCheckUpdate.Elapsed -= timerDelayCheckUpdate_Elapsed; timerDelayCheckUpdate.Stop(); timerDelayCheckUpdate = null; } UpdateItem.Visible = true; UpdateItem.Text = String.Format(I18N.GetString("New version {0} {1} available"), UpdateChecker.Name, updateChecker.LatestVersionNumber); } } void UpdateItem_Clicked(object sender, EventArgs e) { Process.Start(updateChecker.LatestVersionURL); } void notifyIcon1_BalloonTipClicked(object sender, EventArgs e) { Process.Start(updateChecker.LatestVersionURL); _notifyIcon.BalloonTipClicked -= notifyIcon1_BalloonTipClicked; } private void UpdateSysProxyMode(Configuration config) { noModifyItem.Checked = config.sysProxyMode == (int)ProxyMode.NoModify; enableItem.Checked = config.sysProxyMode == (int)ProxyMode.Direct; PACModeItem.Checked = config.sysProxyMode == (int)ProxyMode.Pac; globalModeItem.Checked = config.sysProxyMode == (int)ProxyMode.Global; } private void UpdateProxyRule(Configuration config) { ruleDisableBypass.Checked = config.proxyRuleMode == (int)ProxyRuleMode.Disable; ruleBypassLan.Checked = config.proxyRuleMode == (int)ProxyRuleMode.BypassLan; ruleBypassChina.Checked = config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndChina; ruleBypassNotChina.Checked = config.proxyRuleMode == (int)ProxyRuleMode.BypassLanAndNotChina; ruleUser.Checked = config.proxyRuleMode == (int)ProxyRuleMode.UserCustom; } private void LoadCurrentConfiguration() { Configuration config = _controller.GetCurrentConfiguration(); UpdateServersMenu(); UpdateSysProxyMode(config); UpdateProxyRule(config); sameHostForSameTargetItem.Checked = config.sameHostForSameTarget; } private void UpdateServersMenu() { var items = ServersItem.MenuItems; while (items[0] != SeperatorItem) { items.RemoveAt(0); } Configuration configuration = _controller.GetCurrentConfiguration(); SortedDictionary group = new SortedDictionary(); const string def_group = "!(no group)"; string select_group = ""; for (int i = 0; i < configuration.configs.Count; i++) { string group_name; Server server = configuration.configs[i]; if (string.IsNullOrEmpty(server.group)) group_name = def_group; else group_name = server.group; string latency; if (server.latency == Server.LATENCY_TESTING) { latency = "[testing]"; } else if (server.latency == Server.LATENCY_ERROR) { latency = "[error]"; } else if (server.latency == Server.LATENCY_PENDING) { latency = "[pending]"; } else { latency = "[" + server.latency.ToString() + "ms]"; } MenuItem item = new MenuItem(latency + " " + server.FriendlyName()); item.Tag = i; item.Click += AServerItem_Click; if (configuration.index == i) { item.Checked = true; select_group = group_name; } if (group.ContainsKey(group_name)) { group[group_name].MenuItems.Add(item); } else { group[group_name] = new MenuItem(group_name, new MenuItem[1] { item }); } } { int i = 0; foreach (KeyValuePair pair in group) { if (pair.Key == def_group) { pair.Value.Text = "(empty group)"; } if (pair.Key == select_group) { pair.Value.Text = "● " + pair.Value.Text; } else { pair.Value.Text = " " + pair.Value.Text; } items.Add(i, pair.Value); ++i; } } } private void ShowConfigForm(bool addNode) { if (configForm != null) { configForm.Activate(); if (addNode) { Configuration cfg = _controller.GetCurrentConfiguration(); configForm.SetServerListSelectedIndex(cfg.index + 1); } } else { configfrom_open = true; configForm = new ConfigForm(_controller, updateChecker, addNode ? -1 : -2); configForm.Show(); configForm.Activate(); configForm.BringToFront(); configForm.FormClosed += configForm_FormClosed; } } private void ShowConfigForm(int index) { if (configForm != null) { configForm.Activate(); } else { configfrom_open = true; configForm = new ConfigForm(_controller, updateChecker, index); configForm.Show(); configForm.Activate(); configForm.BringToFront(); configForm.FormClosed += configForm_FormClosed; } } private void ShowSettingForm() { if (settingsForm != null) { settingsForm.Activate(); } else { settingsForm = new SettingsForm(_controller); settingsForm.Show(); settingsForm.Activate(); settingsForm.BringToFront(); settingsForm.FormClosed += settingsForm_FormClosed; } } private void ShowPortMapForm() { if (portMapForm != null) { portMapForm.Activate(); portMapForm.Update(); if (portMapForm.WindowState == FormWindowState.Minimized) { portMapForm.WindowState = FormWindowState.Normal; } } else { portMapForm = new PortSettingsForm(_controller); portMapForm.Show(); portMapForm.Activate(); portMapForm.BringToFront(); portMapForm.FormClosed += portMapForm_FormClosed; } } private void ShowServerLogForm() { if (serverLogForm != null) { serverLogForm.Activate(); serverLogForm.Update(); if (serverLogForm.WindowState == FormWindowState.Minimized) { serverLogForm.WindowState = FormWindowState.Normal; } } else { serverLogForm = new ServerLogForm(_controller); serverLogForm.Show(); serverLogForm.Activate(); serverLogForm.BringToFront(); serverLogForm.FormClosed += serverLogForm_FormClosed; } } private void ShowGlobalLogForm() { if (logForm != null) { logForm.Activate(); logForm.Update(); if (logForm.WindowState == FormWindowState.Minimized) { logForm.WindowState = FormWindowState.Normal; } } else { logForm = new LogForm(_controller); logForm.Show(); logForm.Activate(); logForm.BringToFront(); logForm.FormClosed += globalLogForm_FormClosed; } } private void ShowSubscribeSettingForm() { if (subScribeForm != null) { subScribeForm.Activate(); subScribeForm.Update(); if (subScribeForm.WindowState == FormWindowState.Minimized) { subScribeForm.WindowState = FormWindowState.Normal; } } else { subScribeForm = new SubscribeForm(_controller); subScribeForm.Show(); subScribeForm.Activate(); subScribeForm.BringToFront(); subScribeForm.FormClosed += subScribeForm_FormClosed; } } void configForm_FormClosed(object sender, FormClosedEventArgs e) { configForm = null; configfrom_open = false; Utils.ReleaseMemory(); if (eventList.Count > 0) { foreach (EventParams p in eventList) { updateFreeNodeChecker_NewFreeNodeFound(p._sender, p._e); } eventList.Clear(); } } void settingsForm_FormClosed(object sender, FormClosedEventArgs e) { settingsForm = null; Utils.ReleaseMemory(); } void serverLogForm_FormClosed(object sender, FormClosedEventArgs e) { serverLogForm = null; Util.Utils.ReleaseMemory(); } void portMapForm_FormClosed(object sender, FormClosedEventArgs e) { portMapForm = null; Util.Utils.ReleaseMemory(); } void globalLogForm_FormClosed(object sender, FormClosedEventArgs e) { logForm = null; Util.Utils.ReleaseMemory(); } void subScribeForm_FormClosed(object sender, FormClosedEventArgs e) { subScribeForm = null; timerUpdateLatency = new System.Timers.Timer(1000.0 * 3); timerUpdateLatency.Elapsed += timerUpdateLatency_Elapsed; timerUpdateLatency.Start(); } private void Config_Click(object sender, EventArgs e) { if (typeof(int) == sender.GetType()) { ShowConfigForm((int)sender); } else { ShowConfigForm(false); } } private void Import_Click(object sender, EventArgs e) { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath; if (dlg.ShowDialog() == DialogResult.OK) { string name = dlg.FileName; Configuration cfg = Configuration.LoadFile(name); if (cfg.configs.Count == 1 && cfg.configs[0].server == Configuration.GetDefaultServer().server) { MessageBox.Show("Load config file failed", "ShadowsocksR"); } else { _controller.MergeConfiguration(cfg); LoadCurrentConfiguration(); } } } } private void Setting_Click(object sender, EventArgs e) { ShowSettingForm(); } private void Quit() { _controller.Stop(); if (configForm != null) { configForm.Close(); configForm = null; } if (serverLogForm != null) { serverLogForm.Close(); serverLogForm = null; } if (timerDetectVirus != null) { timerDetectVirus.Elapsed -= timerDetectVirus_Elapsed; timerDetectVirus.Stop(); timerDetectVirus = null; } if (timerDelayCheckUpdate != null) { timerDelayCheckUpdate.Elapsed -= timerDelayCheckUpdate_Elapsed; timerDelayCheckUpdate.Stop(); timerDelayCheckUpdate = null; } if (timerUpdateLatency != null) { timerUpdateLatency.Elapsed -= timerUpdateLatency_Elapsed; timerUpdateLatency.Stop(); timerUpdateLatency = null; } _notifyIcon.Visible = false; Application.Exit(); } private void Quit_Click(object sender, EventArgs e) { Quit(); } private void OpenWiki_Click(object sender, EventArgs e) { Process.Start("https://github.com/shadowsocksrr/shadowsocks-rss/wiki"); } private void FeedbackItem_Click(object sender, EventArgs e) { Process.Start("https://github.com/shadowsocksrr/shadowsocksr-csharp/issues/new"); } private void ResetPasswordItem_Click(object sender, EventArgs e) { ResetPassword dlg = new ResetPassword(); dlg.Show(); dlg.Activate(); } private void AboutItem_Click(object sender, EventArgs e) { Process.Start("https://github.com/SoDa-GitHub/shadowsocksrr-csharp"); } private void DonateItem_Click(object sender, EventArgs e) { Process.Start("https://github.com/SoDa-GitHub/shadowsocksrr-csharp/blob/master/donate.jpg?raw=true"); } [DllImport("user32.dll")] private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); private void notifyIcon1_Click(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { int SCA_key = GetAsyncKeyState(Keys.ShiftKey) < 0 ? 1 : 0; SCA_key |= GetAsyncKeyState(Keys.ControlKey) < 0 ? 2 : 0; SCA_key |= GetAsyncKeyState(Keys.Menu) < 0 ? 4 : 0; if (SCA_key == 2) { ShowServerLogForm(); } else if (SCA_key == 1) { ShowSettingForm(); } else if (SCA_key == 4) { ShowPortMapForm(); } else { ShowConfigForm(false); } } else if (e.Button == MouseButtons.Middle) { ShowServerLogForm(); } } private void NoModifyItem_Click(object sender, EventArgs e) { _controller.ToggleMode(ProxyMode.NoModify); } private void EnableItem_Click(object sender, EventArgs e) { _controller.ToggleMode(ProxyMode.Direct); } private void GlobalModeItem_Click(object sender, EventArgs e) { _controller.ToggleMode(ProxyMode.Global); } private void PACModeItem_Click(object sender, EventArgs e) { _controller.ToggleMode(ProxyMode.Pac); } private void RuleBypassLanItem_Click(object sender, EventArgs e) { _controller.ToggleRuleMode((int)ProxyRuleMode.BypassLan); } private void RuleBypassChinaItem_Click(object sender, EventArgs e) { _controller.ToggleRuleMode((int)ProxyRuleMode.BypassLanAndChina); } private void RuleBypassNotChinaItem_Click(object sender, EventArgs e) { _controller.ToggleRuleMode((int)ProxyRuleMode.BypassLanAndNotChina); } private void RuleUserItem_Click(object sender, EventArgs e) { _controller.ToggleRuleMode((int)ProxyRuleMode.UserCustom); } private void RuleBypassDisableItem_Click(object sender, EventArgs e) { _controller.ToggleRuleMode((int)ProxyRuleMode.Disable); } private void SelectSameHostForSameTargetItem_Click(object sender, EventArgs e) { sameHostForSameTargetItem.Checked = !sameHostForSameTargetItem.Checked; _controller.ToggleSameHostForSameTargetRandom(sameHostForSameTargetItem.Checked); } private void CopyPACURLItem_Click(object sender, EventArgs e) { try { Configuration config = _controller.GetCurrentConfiguration(); string pacUrl; pacUrl = "http://127.0.0.1:" + config.localPort.ToString() + "/pac?" + "auth=" + config.localAuthPassword + "&t=" + Util.Utils.GetTimestamp(DateTime.Now); Clipboard.SetText(pacUrl); } catch { } } private void EditPACFileItem_Click(object sender, EventArgs e) { _controller.TouchPACFile(); } private void UpdatePACFromGFWListItem_Click(object sender, EventArgs e) { _controller.UpdatePACFromGFWList(); } private void UpdatePACFromLanIPListItem_Click(object sender, EventArgs e) { _controller.UpdatePACFromOnlinePac("https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/ss_lanip.pac"); } private void UpdatePACFromCNWhiteListItem_Click(object sender, EventArgs e) { _controller.UpdatePACFromOnlinePac("https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/ss_white.pac"); } private void UpdatePACFromCNOnlyListItem_Click(object sender, EventArgs e) { _controller.UpdatePACFromOnlinePac("https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/ss_white_r.pac"); } private void UpdatePACFromCNIPListItem_Click(object sender, EventArgs e) { _controller.UpdatePACFromOnlinePac("https://raw.githubusercontent.com/shadowsocksrr/breakwa11.github.io/master/ssr/ss_cnip.pac"); } private void EditUserRuleFileForGFWListItem_Click(object sender, EventArgs e) { _controller.TouchUserRuleFile(); } private void AServerItem_Click(object sender, EventArgs e) { MenuItem item = (MenuItem)sender; _controller.SelectServerIndex((int)item.Tag); Configuration config = _controller.GetCurrentConfiguration(); for (int id = 0; id < config.configs.Count; ++id) { Server server = config.configs[id]; server.GetConnections().CloseAll(); } } private void CheckUpdate_Click(object sender, EventArgs e) { updateChecker.CheckUpdate(_controller.GetCurrentConfiguration()); } private void CheckNodeUpdateUseProxy_Click(object sender, EventArgs e) { updateSubscribeManager.CreateTask(_controller.GetCurrentConfiguration(), updateFreeNodeChecker, -1, true, true); } private void CheckNodeUpdate_Click(object sender, EventArgs e) { updateSubscribeManager.CreateTask(_controller.GetCurrentConfiguration(), updateFreeNodeChecker, -1, false, true); } private void ShowLogItem_Click(object sender, EventArgs e) { ShowGlobalLogForm(); } private void ShowPortMapItem_Click(object sender, EventArgs e) { ShowPortMapForm(); } private void ShowServerLogItem_Click(object sender, EventArgs e) { ShowServerLogForm(); } private void SubscribeSetting_Click(object sender, EventArgs e) { ShowSubscribeSettingForm(); } private void DisconnectCurrent_Click(object sender, EventArgs e) { Configuration config = _controller.GetCurrentConfiguration(); for (int id = 0; id < config.configs.Count; ++id) { Server server = config.configs[id]; server.GetConnections().CloseAll(); } } private void URL_Split(string text, ref List out_urls) { if (String.IsNullOrEmpty(text)) { return; } int ss_index = text.IndexOf("ss://", 1, StringComparison.OrdinalIgnoreCase); int ssr_index = text.IndexOf("ssr://", 1, StringComparison.OrdinalIgnoreCase); int index = ss_index; if (index == -1 || index > ssr_index && ssr_index != -1) index = ssr_index; if (index == -1) { out_urls.Insert(0, text); } else { out_urls.Insert(0, text.Substring(0, index)); URL_Split(text.Substring(index), ref out_urls); } } private void CopyAddress_Click(object sender, EventArgs e) { try { IDataObject iData = Clipboard.GetDataObject(); if (iData.GetDataPresent(DataFormats.Text)) { List urls = new List(); URL_Split((string)iData.GetData(DataFormats.Text), ref urls); int count = 0; foreach (string url in urls) { if (_controller.AddServerBySSURL(url)) ++count; } if (count > 0) ShowConfigForm(true); } } catch { } } private bool ScanQRCode(Screen screen, Bitmap fullImage, Rectangle cropRect, out string url, out Rectangle rect) { using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height)) { using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(fullImage, new Rectangle(0, 0, cropRect.Width, cropRect.Height), cropRect, GraphicsUnit.Pixel); } var source = new BitmapLuminanceSource(target); var bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); var result = reader.decode(bitmap); if (result != null) { url = result.Text; double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; foreach (ResultPoint point in result.ResultPoints) { minX = Math.Min(minX, point.X); minY = Math.Min(minY, point.Y); maxX = Math.Max(maxX, point.X); maxY = Math.Max(maxY, point.Y); } //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); rect = new Rectangle(cropRect.Left + (int)minX, cropRect.Top + (int)minY, (int)(maxX - minX), (int)(maxY - minY)); return true; } } url = ""; rect = new Rectangle(); return false; } private bool ScanQRCodeStretch(Screen screen, Bitmap fullImage, Rectangle cropRect, double mul, out string url, out Rectangle rect) { using (Bitmap target = new Bitmap((int)(cropRect.Width * mul), (int)(cropRect.Height * mul))) { using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } var source = new BitmapLuminanceSource(target); var bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); var result = reader.decode(bitmap); if (result != null) { url = result.Text; double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0; foreach (ResultPoint point in result.ResultPoints) { minX = Math.Min(minX, point.X); minY = Math.Min(minY, point.Y); maxX = Math.Max(maxX, point.X); maxY = Math.Max(maxY, point.Y); } //rect = new Rectangle((int)minX, (int)minY, (int)(maxX - minX), (int)(maxY - minY)); rect = new Rectangle(cropRect.Left + (int)(minX / mul), cropRect.Top + (int)(minY / mul), (int)((maxX - minX) / mul), (int)((maxY - minY) / mul)); return true; } } url = ""; rect = new Rectangle(); return false; } private Rectangle GetScanRect(int width, int height, int index, out double stretch) { stretch = 1; if (index < 5) { const int div = 5; int w = width * 3 / div; int h = height * 3 / div; Point[] pt = new Point[5] { new Point(1, 1), new Point(0, 0), new Point(0, 2), new Point(2, 0), new Point(2, 2), }; return new Rectangle(pt[index].X * width / div, pt[index].Y * height / div, w, h); } { const int base_index = 5; if (index < base_index + 6) { double[] s = new double[] { 1, 2, 3, 4, 6, 8 }; stretch = 1 / s[index - base_index]; return new Rectangle(0, 0, width, height); } } { const int base_index = 11; if (index < base_index + 8) { const int hdiv = 7; const int vdiv = 5; int w = width * 3 / hdiv; int h = height * 3 / vdiv; Point[] pt = new Point[8] { new Point(1, 1), new Point(3, 1), new Point(0, 0), new Point(0, 2), new Point(2, 0), new Point(2, 2), new Point(4, 0), new Point(4, 2), }; return new Rectangle(pt[index - base_index].X * width / hdiv, pt[index - base_index].Y * height / vdiv, w, h); } } return new Rectangle(0, 0, 0, 0); } private void ScanScreenQRCode(bool ss_only) { Thread.Sleep(100); foreach (Screen screen in Screen.AllScreens) { Point screen_size = Util.Utils.GetScreenPhysicalSize(); using (Bitmap fullImage = new Bitmap(screen_size.X, screen_size.Y)) { using (Graphics g = Graphics.FromImage(fullImage)) { g.CopyFromScreen(screen.Bounds.X, screen.Bounds.Y, 0, 0, fullImage.Size, CopyPixelOperation.SourceCopy); } bool decode_fail = false; for (int i = 0; i < 100; i++) { double stretch; Rectangle cropRect = GetScanRect(fullImage.Width, fullImage.Height, i, out stretch); if (cropRect.Width == 0) break; string url; Rectangle rect; if (stretch == 1 ? ScanQRCode(screen, fullImage, cropRect, out url, out rect) : ScanQRCodeStretch(screen, fullImage, cropRect, stretch, out url, out rect)) { var success = _controller.AddServerBySSURL(url); QRCodeSplashForm splash = new QRCodeSplashForm(); if (success) { splash.FormClosed += splash_FormClosed; } else if (!ss_only) { _urlToOpen = url; //if (url.StartsWith("http://") || url.StartsWith("https://")) // splash.FormClosed += openURLFromQRCode; //else splash.FormClosed += showURLFromQRCode; } else { decode_fail = true; continue; } splash.Location = new Point(screen.Bounds.X, screen.Bounds.Y); double dpi = Screen.PrimaryScreen.Bounds.Width / (double)screen_size.X; splash.TargetRect = new Rectangle( (int)(rect.Left * dpi + screen.Bounds.X), (int)(rect.Top * dpi + screen.Bounds.Y), (int)(rect.Width * dpi), (int)(rect.Height * dpi)); splash.Size = new Size(fullImage.Width, fullImage.Height); splash.Show(); return; } } if (decode_fail) { MessageBox.Show(I18N.GetString("Failed to decode QRCode")); return; } } } MessageBox.Show(I18N.GetString("No QRCode found. Try to zoom in or move it to the center of the screen.")); } private void ScanQRCodeItem_Click(object sender, EventArgs e) { ScanScreenQRCode(false); } void splash_FormClosed(object sender, FormClosedEventArgs e) { ShowConfigForm(true); } void openURLFromQRCode(object sender, FormClosedEventArgs e) { Process.Start(_urlToOpen); } void showURLFromQRCode() { ShowTextForm dlg = new ShowTextForm("QRCode", _urlToOpen); dlg.Show(); dlg.Activate(); dlg.BringToFront(); } void showURLFromQRCode(object sender, FormClosedEventArgs e) { showURLFromQRCode(); } void showURLFromQRCode(object sender, System.EventArgs e) { showURLFromQRCode(); } } } ================================================ FILE: shadowsocks-csharp/View/PortSettingsForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class PortSettingsForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.listPorts = new System.Windows.Forms.ListBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.labelType = new System.Windows.Forms.Label(); this.labelID = new System.Windows.Forms.Label(); this.labelAddr = new System.Windows.Forms.Label(); this.labelPort = new System.Windows.Forms.Label(); this.checkEnable = new System.Windows.Forms.CheckBox(); this.textAddr = new System.Windows.Forms.TextBox(); this.NumTargetPort = new System.Windows.Forms.NumericUpDown(); this.comboBoxType = new System.Windows.Forms.ComboBox(); this.comboServers = new System.Windows.Forms.ComboBox(); this.labelLocal = new System.Windows.Forms.Label(); this.NumLocalPort = new System.Windows.Forms.NumericUpDown(); this.label1 = new System.Windows.Forms.Label(); this.textRemarks = new System.Windows.Forms.TextBox(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.OKButton = new System.Windows.Forms.Button(); this.MyCancelButton = new System.Windows.Forms.Button(); this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); this.Add = new System.Windows.Forms.Button(); this.Del = new System.Windows.Forms.Button(); this.tableLayoutPanel1.SuspendLayout(); this.groupBox1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumTargetPort)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NumLocalPort)).BeginInit(); this.tableLayoutPanel3.SuspendLayout(); this.tableLayoutPanel4.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 // this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.listPorts, 0, 0); this.tableLayoutPanel1.Controls.Add(this.groupBox1, 1, 0); this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel3, 1, 1); this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel4, 0, 1); this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 13); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 2; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.Size = new System.Drawing.Size(684, 345); this.tableLayoutPanel1.TabIndex = 0; // // listPorts // this.listPorts.FormattingEnabled = true; this.listPorts.ItemHeight = 20; this.listPorts.Location = new System.Drawing.Point(3, 3); this.listPorts.Name = "listPorts"; this.listPorts.Size = new System.Drawing.Size(144, 244); this.listPorts.TabIndex = 0; this.listPorts.SelectedIndexChanged += new System.EventHandler(this.listPorts_SelectedIndexChanged); // // groupBox1 // this.groupBox1.Controls.Add(this.tableLayoutPanel2); this.groupBox1.Location = new System.Drawing.Point(153, 3); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(531, 255); this.groupBox1.TabIndex = 2; this.groupBox1.TabStop = false; this.groupBox1.Text = "Map Setting"; // // tableLayoutPanel2 // this.tableLayoutPanel2.ColumnCount = 3; this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.Controls.Add(this.labelType, 0, 1); this.tableLayoutPanel2.Controls.Add(this.labelID, 0, 2); this.tableLayoutPanel2.Controls.Add(this.labelAddr, 0, 4); this.tableLayoutPanel2.Controls.Add(this.labelPort, 0, 5); this.tableLayoutPanel2.Controls.Add(this.checkEnable, 1, 0); this.tableLayoutPanel2.Controls.Add(this.textAddr, 1, 4); this.tableLayoutPanel2.Controls.Add(this.NumTargetPort, 1, 5); this.tableLayoutPanel2.Controls.Add(this.comboBoxType, 1, 1); this.tableLayoutPanel2.Controls.Add(this.comboServers, 1, 2); this.tableLayoutPanel2.Controls.Add(this.labelLocal, 0, 3); this.tableLayoutPanel2.Controls.Add(this.NumLocalPort, 1, 3); this.tableLayoutPanel2.Controls.Add(this.label1, 0, 6); this.tableLayoutPanel2.Controls.Add(this.textRemarks, 1, 6); this.tableLayoutPanel2.Location = new System.Drawing.Point(7, 21); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; this.tableLayoutPanel2.RowCount = 8; this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.Size = new System.Drawing.Size(515, 223); this.tableLayoutPanel2.TabIndex = 0; // // labelType // this.labelType.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelType.AutoSize = true; this.labelType.Location = new System.Drawing.Point(53, 37); this.labelType.Name = "labelType"; this.labelType.Size = new System.Drawing.Size(43, 20); this.labelType.TabIndex = 0; this.labelType.Text = "Type"; // // labelID // this.labelID.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelID.AutoSize = true; this.labelID.Location = new System.Drawing.Point(20, 71); this.labelID.Name = "labelID"; this.labelID.Size = new System.Drawing.Size(76, 20); this.labelID.TabIndex = 0; this.labelID.Text = "Server ID"; // // labelAddr // this.labelAddr.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelAddr.AutoSize = true; this.labelAddr.Location = new System.Drawing.Point(3, 136); this.labelAddr.Name = "labelAddr"; this.labelAddr.Size = new System.Drawing.Size(93, 20); this.labelAddr.TabIndex = 0; this.labelAddr.Text = "Target Addr"; // // labelPort // this.labelPort.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelPort.AutoSize = true; this.labelPort.Location = new System.Drawing.Point(8, 168); this.labelPort.Name = "labelPort"; this.labelPort.Size = new System.Drawing.Size(88, 20); this.labelPort.TabIndex = 0; this.labelPort.Text = "Target Port"; // // checkEnable // this.checkEnable.AutoSize = true; this.checkEnable.Location = new System.Drawing.Point(102, 3); this.checkEnable.Name = "checkEnable"; this.checkEnable.Size = new System.Drawing.Size(85, 24); this.checkEnable.TabIndex = 3; this.checkEnable.Text = "Enable"; this.checkEnable.UseVisualStyleBackColor = true; // // textAddr // this.textAddr.Location = new System.Drawing.Point(102, 133); this.textAddr.Name = "textAddr"; this.textAddr.Size = new System.Drawing.Size(403, 26); this.textAddr.TabIndex = 7; // // NumTargetPort // this.NumTargetPort.Location = new System.Drawing.Point(102, 165); this.NumTargetPort.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumTargetPort.Name = "NumTargetPort"; this.NumTargetPort.Size = new System.Drawing.Size(403, 26); this.NumTargetPort.TabIndex = 8; // // comboBoxType // this.comboBoxType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxType.FormattingEnabled = true; this.comboBoxType.Items.AddRange(new object[] { "Port Forward", "Force Proxy", "Proxy With Rule"}); this.comboBoxType.Location = new System.Drawing.Point(102, 33); this.comboBoxType.Name = "comboBoxType"; this.comboBoxType.Size = new System.Drawing.Size(403, 28); this.comboBoxType.TabIndex = 4; this.comboBoxType.SelectedIndexChanged += new System.EventHandler(this.comboBoxType_SelectedIndexChanged); // // comboServers // this.comboServers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboServers.FormattingEnabled = true; this.comboServers.Location = new System.Drawing.Point(102, 67); this.comboServers.Name = "comboServers"; this.comboServers.Size = new System.Drawing.Size(403, 28); this.comboServers.TabIndex = 5; // // labelLocal // this.labelLocal.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelLocal.AutoSize = true; this.labelLocal.Location = new System.Drawing.Point(16, 104); this.labelLocal.Name = "labelLocal"; this.labelLocal.Size = new System.Drawing.Size(80, 20); this.labelLocal.TabIndex = 0; this.labelLocal.Text = "Local Port"; // // NumLocalPort // this.NumLocalPort.Location = new System.Drawing.Point(102, 101); this.NumLocalPort.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumLocalPort.Name = "NumLocalPort"; this.NumLocalPort.Size = new System.Drawing.Size(403, 26); this.NumLocalPort.TabIndex = 6; // // label1 // this.label1.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(23, 200); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(73, 20); this.label1.TabIndex = 0; this.label1.Text = "Remarks"; // // textRemarks // this.textRemarks.Location = new System.Drawing.Point(102, 197); this.textRemarks.Name = "textRemarks"; this.textRemarks.Size = new System.Drawing.Size(403, 26); this.textRemarks.TabIndex = 9; // // tableLayoutPanel3 // this.tableLayoutPanel3.ColumnCount = 2; this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); this.tableLayoutPanel3.Location = new System.Drawing.Point(153, 264); this.tableLayoutPanel3.Name = "tableLayoutPanel3"; this.tableLayoutPanel3.RowCount = 1; this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel3.Size = new System.Drawing.Size(490, 54); this.tableLayoutPanel3.TabIndex = 3; // // OKButton // this.OKButton.Anchor = System.Windows.Forms.AnchorStyles.Top; this.OKButton.Location = new System.Drawing.Point(46, 3); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(152, 35); this.OKButton.TabIndex = 10; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; this.OKButton.Click += new System.EventHandler(this.OKButton_Click); // // MyCancelButton // this.MyCancelButton.Anchor = System.Windows.Forms.AnchorStyles.Top; this.MyCancelButton.Location = new System.Drawing.Point(291, 3); this.MyCancelButton.Name = "MyCancelButton"; this.MyCancelButton.Size = new System.Drawing.Size(152, 35); this.MyCancelButton.TabIndex = 11; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); // // tableLayoutPanel4 // this.tableLayoutPanel4.ColumnCount = 1; this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel4.Controls.Add(this.Add, 0, 0); this.tableLayoutPanel4.Controls.Add(this.Del, 0, 1); this.tableLayoutPanel4.Location = new System.Drawing.Point(3, 264); this.tableLayoutPanel4.Name = "tableLayoutPanel4"; this.tableLayoutPanel4.RowCount = 2; this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.Size = new System.Drawing.Size(144, 81); this.tableLayoutPanel4.TabIndex = 1; // // Add // this.Add.Anchor = System.Windows.Forms.AnchorStyles.Top; this.Add.Location = new System.Drawing.Point(22, 3); this.Add.Name = "Add"; this.Add.Size = new System.Drawing.Size(100, 34); this.Add.TabIndex = 1; this.Add.Text = "&Add"; this.Add.UseVisualStyleBackColor = true; this.Add.Click += new System.EventHandler(this.Add_Click); // // Del // this.Del.Anchor = System.Windows.Forms.AnchorStyles.Top; this.Del.Location = new System.Drawing.Point(22, 43); this.Del.Name = "Del"; this.Del.Size = new System.Drawing.Size(100, 35); this.Del.TabIndex = 2; this.Del.Text = "&Delete"; this.Del.UseVisualStyleBackColor = true; this.Del.Click += new System.EventHandler(this.Del_Click); // // PortSettingsForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ClientSize = new System.Drawing.Size(727, 489); this.Controls.Add(this.tableLayoutPanel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "PortSettingsForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Port Settings"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.PortMapForm_FormClosed); this.tableLayoutPanel1.ResumeLayout(false); this.groupBox1.ResumeLayout(false); this.tableLayoutPanel2.ResumeLayout(false); this.tableLayoutPanel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumTargetPort)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.NumLocalPort)).EndInit(); this.tableLayoutPanel3.ResumeLayout(false); this.tableLayoutPanel4.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.ListBox listPorts; private System.Windows.Forms.Button OKButton; private System.Windows.Forms.Button MyCancelButton; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.Label labelType; private System.Windows.Forms.Label labelID; private System.Windows.Forms.Label labelAddr; private System.Windows.Forms.Label labelPort; private System.Windows.Forms.CheckBox checkEnable; private System.Windows.Forms.TextBox textAddr; private System.Windows.Forms.NumericUpDown NumTargetPort; private System.Windows.Forms.ComboBox comboBoxType; private System.Windows.Forms.ComboBox comboServers; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; private System.Windows.Forms.Label labelLocal; private System.Windows.Forms.NumericUpDown NumLocalPort; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; private System.Windows.Forms.Button Add; private System.Windows.Forms.Button Del; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textRemarks; } } ================================================ FILE: shadowsocks-csharp/View/PortSettingsForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; namespace Shadowsocks.View { public partial class PortSettingsForm : Form { private ShadowsocksController controller; private Configuration _modifiedConfiguration; private int _oldSelectedIndex = -1; public PortSettingsForm(ShadowsocksController controller) { InitializeComponent(); this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.controller = controller; controller.ConfigChanged += controller_ConfigChanged; UpdateTexts(); comboBoxType.DisplayMember = "Text"; comboBoxType.ValueMember = "Value"; var items = new[] { new {Text = I18N.GetString("Port Forward"), Value = PortMapType.Forward}, new {Text = I18N.GetString("Force Proxy"), Value = PortMapType.ForceProxy}, new {Text = I18N.GetString("Proxy With Rule"), Value = PortMapType.RuleProxy} }; comboBoxType.DataSource = items; LoadCurrentConfiguration(); } private void UpdateTexts() { this.Text = I18N.GetString("Port Settings"); groupBox1.Text = I18N.GetString("Map Setting"); labelType.Text = I18N.GetString("Type"); labelID.Text = I18N.GetString("Server ID"); labelAddr.Text = I18N.GetString("Target Addr"); labelPort.Text = I18N.GetString("Target Port"); checkEnable.Text = I18N.GetString("Enable"); labelLocal.Text = I18N.GetString("Local Port"); label1.Text = I18N.GetString("Remarks"); OKButton.Text = I18N.GetString("OK"); MyCancelButton.Text = I18N.GetString("Cancel"); Add.Text = I18N.GetString("&Add"); Del.Text = I18N.GetString("&Delete"); } private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); } private void PortMapForm_FormClosed(object sender, FormClosedEventArgs e) { controller.ConfigChanged -= controller_ConfigChanged; } private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfiguration(); LoadConfiguration(_modifiedConfiguration); LoadSelectedServer(); } private void OKButton_Click(object sender, EventArgs e) { SaveSelectedServer(); controller.SaveServersPortMap(_modifiedConfiguration); this.Close(); } private void CancelButton_Click(object sender, EventArgs e) { this.Close(); } private void LoadConfiguration(Configuration configuration) { comboServers.Items.Clear(); comboServers.Items.Add(""); Dictionary server_group = new Dictionary(); foreach (Server s in configuration.configs) { if (!string.IsNullOrEmpty(s.group) && !server_group.ContainsKey(s.group)) { comboServers.Items.Add("#" + s.group); server_group[s.group] = 1; } } foreach (Server s in configuration.configs) { comboServers.Items.Add(GetDisplayText(s)); } listPorts.Items.Clear(); int[] list = new int[configuration.portMap.Count]; int list_index = 0; foreach (KeyValuePair it in configuration.portMap) { try { list[list_index] = int.Parse(it.Key); } catch (FormatException) { } list_index += 1; } Array.Sort(list); for (int i = 0; i < list.Length; ++i) { string remarks = ""; remarks = ((PortMapConfig)configuration.portMap[list[i].ToString()]).remarks ?? ""; listPorts.Items.Add(list[i].ToString() + " " + remarks); } _oldSelectedIndex = -1; if (listPorts.Items.Count > 0) { listPorts.SelectedIndex = 0; } } private string ServerListText2Key(string text) { if (text != null) { int pos = text.IndexOf(' '); if (pos > 0) return text.Substring(0, pos); } return text; } private void SaveSelectedServer() { if (_oldSelectedIndex != -1) { bool reflash_list = false; string key = _oldSelectedIndex.ToString(); if (key != NumLocalPort.Text) { if (_modifiedConfiguration.portMap.ContainsKey(key)) { _modifiedConfiguration.portMap.Remove(key); } reflash_list = true; key = NumLocalPort.Text; try { _oldSelectedIndex = int.Parse(key); } catch (FormatException) { _oldSelectedIndex = 0; } } if (!_modifiedConfiguration.portMap.ContainsKey(key)) { _modifiedConfiguration.portMap[key] = new PortMapConfig(); } PortMapConfig cfg = _modifiedConfiguration.portMap[key] as PortMapConfig; cfg.enable = checkEnable.Checked; cfg.type = (PortMapType) comboBoxType.SelectedValue; cfg.id = GetID(comboServers.Text); cfg.server_addr = textAddr.Text; if (cfg.remarks != textRemarks.Text) { reflash_list = true; } cfg.remarks = textRemarks.Text; cfg.server_port = Convert.ToInt32(NumTargetPort.Value); if (reflash_list) { LoadConfiguration(_modifiedConfiguration); } } } private void LoadSelectedServer() { string key = ServerListText2Key((string)listPorts.SelectedItem); Dictionary server_group = new Dictionary(); foreach (Server s in _modifiedConfiguration.configs) { if (!string.IsNullOrEmpty(s.group) && !server_group.ContainsKey(s.group)) { server_group[s.group] = 1; } } if (key != null && _modifiedConfiguration.portMap.ContainsKey(key)) { PortMapConfig cfg = _modifiedConfiguration.portMap[key] as PortMapConfig; checkEnable.Checked = cfg.enable; comboBoxType.SelectedValue = cfg.type; string text = GetIDText(cfg.id); if (text.Length == 0 && server_group.ContainsKey(cfg.id)) { text = "#" + cfg.id; } comboServers.Text = text; NumLocalPort.Text = key; textAddr.Text = cfg.server_addr; NumTargetPort.Value = cfg.server_port; textRemarks.Text = cfg.remarks ?? ""; try { _oldSelectedIndex = int.Parse(key); } catch (FormatException) { _oldSelectedIndex = 0; } } } private string GetID(string text) { if (text.IndexOf('#') >= 0) { return text.Substring(text.IndexOf('#') + 1); } return text; } private string GetDisplayText(Server s) { return (!string.IsNullOrEmpty(s.group) ? s.group + " - " : " - ") + s.FriendlyName() + " #" + s.id; } private string GetIDText(string id) { foreach (Server s in _modifiedConfiguration.configs) { if (id == s.id) { return GetDisplayText(s); } } return ""; } private void listPorts_SelectedIndexChanged(object sender, EventArgs e) { SaveSelectedServer(); LoadSelectedServer(); } private void Add_Click(object sender, EventArgs e) { SaveSelectedServer(); string key = "0"; if (!_modifiedConfiguration.portMap.ContainsKey(key)) { _modifiedConfiguration.portMap[key] = new PortMapConfig(); } PortMapConfig cfg = _modifiedConfiguration.portMap[key] as PortMapConfig; cfg.enable = checkEnable.Checked; cfg.type = (PortMapType) comboBoxType.SelectedValue; cfg.id = GetID(comboServers.Text); cfg.server_addr = textAddr.Text; cfg.remarks = textRemarks.Text; cfg.server_port = Convert.ToInt32(NumTargetPort.Value); _oldSelectedIndex = -1; LoadConfiguration(_modifiedConfiguration); LoadSelectedServer(); } private void Del_Click(object sender, EventArgs e) { string key = _oldSelectedIndex.ToString(); if (_modifiedConfiguration.portMap.ContainsKey(key)) { _modifiedConfiguration.portMap.Remove(key); } _oldSelectedIndex = -1; LoadConfiguration(_modifiedConfiguration); LoadSelectedServer(); } private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxType.SelectedIndex == 0) { textAddr.ReadOnly = false; NumTargetPort.ReadOnly = false; NumTargetPort.Increment = 1; } else { textAddr.ReadOnly = true; NumTargetPort.ReadOnly = true; NumTargetPort.Increment = 0; } } } } ================================================ FILE: shadowsocks-csharp/View/PortSettingsForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/QRCodeSplashForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Diagnostics; namespace Shadowsocks.View { public class QRCodeSplashForm : PerPixelAlphaForm { public Rectangle TargetRect; public QRCodeSplashForm() { this.Load += QRCodeSplashForm_Load; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(1, 1); this.ControlBox = false; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "QRCodeSplashForm"; this.ShowIcon = false; this.ShowInTaskbar = false; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.TopMost = true; } private Timer timer; private int flashStep; private static double FPS = 1.0 / 15 * 1000; // System.Windows.Forms.Timer resolution is 15ms private static double ANIMATION_TIME = 0.5; private static int ANIMATION_STEPS = (int)(ANIMATION_TIME * FPS); Stopwatch sw; int x; int y; int w; int h; Bitmap bitmap; Graphics g; Pen pen; SolidBrush brush; private void QRCodeSplashForm_Load(object sender, EventArgs e) { SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; flashStep = 0; x = 0; y = 0; w = Width; h = Height; sw = Stopwatch.StartNew(); timer = new Timer(); timer.Interval = (int)(ANIMATION_TIME * 1000 / ANIMATION_STEPS); timer.Tick += timer_Tick; timer.Start(); bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); g = Graphics.FromImage(bitmap); pen = new Pen(Color.Red, 3); brush = new SolidBrush(Color.FromArgb(30, Color.Red)); } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style return cp; } } void timer_Tick(object sender, EventArgs e) { double percent = (double)sw.ElapsedMilliseconds / 1000.0 / (double)ANIMATION_TIME; if (percent < 1) { // ease out percent = 1 - Math.Pow((1 - percent), 4); x = (int)(TargetRect.X * percent); y = (int)(TargetRect.Y * percent); w = (int)(TargetRect.Width * percent + this.Size.Width * (1 - percent)); h = (int)(TargetRect.Height * percent + this.Size.Height * (1 - percent)); //codeRectView.Location = new Point(x, y); //codeRectView.Size = new Size(w, h); pen.Color = Color.FromArgb((int)(255 * percent), Color.Red); brush.Color = Color.FromArgb((int)(30 * percent), Color.Red); g.Clear(Color.Transparent); g.FillRectangle(brush, x, y, w, h); g.DrawRectangle(pen, x, y, w, h); SetBitmap(bitmap); } else { if (flashStep == 0) { timer.Interval = 100; g.Clear(Color.Transparent); SetBitmap(bitmap); } else if (flashStep == 1) { timer.Interval = 50; g.FillRectangle(brush, x, y, w, h); g.DrawRectangle(pen, x, y, w, h); SetBitmap(bitmap); } else if (flashStep == 1) { g.Clear(Color.Transparent); SetBitmap(bitmap); } else if (flashStep == 2) { g.FillRectangle(brush, x, y, w, h); g.DrawRectangle(pen, x, y, w, h); SetBitmap(bitmap); } else if (flashStep == 3) { g.Clear(Color.Transparent); SetBitmap(bitmap); } else if (flashStep == 4) { g.FillRectangle(brush, x, y, w, h); g.DrawRectangle(pen, x, y, w, h); SetBitmap(bitmap); } else { sw.Stop(); timer.Stop(); pen.Dispose(); brush.Dispose(); bitmap.Dispose(); this.Close(); } flashStep++; } } } // class that exposes needed win32 gdi functions. class Win32 { [StructLayout(LayoutKind.Sequential)] public struct Point { public Int32 x; public Int32 y; public Point(Int32 x, Int32 y) { this.x = x; this.y = y; } } [StructLayout(LayoutKind.Sequential)] public struct Size { public Int32 cx; public Int32 cy; public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; } } [StructLayout(LayoutKind.Sequential, Pack = 1)] struct ARGB { public byte Blue; public byte Green; public byte Red; public byte Alpha; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; } public const Int32 ULW_COLORKEY = 0x00000001; public const Int32 ULW_ALPHA = 0x00000002; public const Int32 ULW_OPAQUE = 0x00000004; public const byte AC_SRC_OVER = 0x00; public const byte AC_SRC_ALPHA = 0x01; [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll", ExactSpelling = true)] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern int DeleteDC(IntPtr hdc); [DllImport("gdi32.dll", ExactSpelling = true)] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)] public static extern int DeleteObject(IntPtr hObject); } public class PerPixelAlphaForm : Form { // http://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C // Rui Lopes public PerPixelAlphaForm() { // This form should not have a border or else Windows will clip it. FormBorderStyle = FormBorderStyle.None; } public void SetBitmap(Bitmap bitmap) { SetBitmap(bitmap, 255); } /// Changes the current bitmap with a custom opacity level. Here is where all happens! public void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); // The idea of this is very simple, // 1. Create a compatible DC with screen; // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC; // 3. Call the UpdateLayeredWindow. IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak. Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } } protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style return cp; } } } } ================================================ FILE: shadowsocks-csharp/View/ResetPassword.Designer.cs ================================================ namespace Shadowsocks.View { partial class ResetPassword { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.buttonOK = new System.Windows.Forms.Button(); this.textOld = new System.Windows.Forms.TextBox(); this.textPassword = new System.Windows.Forms.TextBox(); this.textPassword2 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // buttonOK // this.buttonOK.Location = new System.Drawing.Point(16, 142); this.buttonOK.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(397, 38); this.buttonOK.TabIndex = 3; this.buttonOK.Text = "OK"; this.buttonOK.UseVisualStyleBackColor = true; this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click); // // textOld // this.textOld.Location = new System.Drawing.Point(159, 34); this.textOld.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.textOld.Name = "textOld"; this.textOld.Size = new System.Drawing.Size(257, 26); this.textOld.TabIndex = 0; this.textOld.UseSystemPasswordChar = true; // // textPassword // this.textPassword.Location = new System.Drawing.Point(159, 70); this.textPassword.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.textPassword.Name = "textPassword"; this.textPassword.Size = new System.Drawing.Size(257, 26); this.textPassword.TabIndex = 1; this.textPassword.UseSystemPasswordChar = true; // // textPassword2 // this.textPassword2.Location = new System.Drawing.Point(159, 106); this.textPassword2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.textPassword2.Name = "textPassword2"; this.textPassword2.Size = new System.Drawing.Size(257, 26); this.textPassword2.TabIndex = 2; this.textPassword2.UseSystemPasswordChar = true; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 37); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(106, 20); this.label2.TabIndex = 5; this.label2.Text = "Old Password"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 73); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(113, 20); this.label1.TabIndex = 6; this.label1.Text = "New Password"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(12, 109); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(137, 20); this.label3.TabIndex = 7; this.label3.Text = "Confirm Password"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(12, 9); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(343, 20); this.label4.TabIndex = 8; this.label4.Text = "This password is use to encrypt local SSR data."; // // ResetPassword // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(426, 191); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label1); this.Controls.Add(this.label2); this.Controls.Add(this.textPassword2); this.Controls.Add(this.textPassword); this.Controls.Add(this.textOld); this.Controls.Add(this.buttonOK); this.ImeMode = System.Windows.Forms.ImeMode.Off; this.KeyPreview = true; this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "ResetPassword"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ResetPassword"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ResetPassword_KeyDown); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.TextBox textOld; private System.Windows.Forms.TextBox textPassword; private System.Windows.Forms.TextBox textPassword2; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; } } ================================================ FILE: shadowsocks-csharp/View/ResetPassword.cs ================================================ using System; using System.Drawing; using System.Windows.Forms; using Shadowsocks.Model; using Shadowsocks.Properties; using Shadowsocks.Controller; namespace Shadowsocks.View { public partial class ResetPassword : Form { public ResetPassword() { InitializeComponent(); this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.Text = I18N.GetString("ResetPassword"); } private void buttonOK_Click(object sender, EventArgs e) { if (textPassword.Text == textPassword2.Text && Configuration.SetPasswordTry(textOld.Text, textPassword.Text)) { Configuration cfg = Configuration.Load(); Configuration.SetPassword(textPassword.Text); Configuration.Save(cfg); Close(); } else { MessageBox.Show(I18N.GetString("Password NOT match"), "SSR error", MessageBoxButtons.OK); } } private void ResetPassword_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (textOld.Focused) { textPassword.Focus(); } else if (textPassword.Focused) { textPassword2.Focus(); } else { buttonOK_Click(this, new EventArgs()); } } } } } ================================================ FILE: shadowsocks-csharp/View/ResetPassword.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/ServerLogForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class ServerLogForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle7 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle8 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle9 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle10 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle11 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle12 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle13 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle14 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle15 = new System.Windows.Forms.DataGridViewCellStyle(); this.ServerDataGrid = new Shadowsocks.View.ServerLogForm.DoubleBufferListView(); this.timer = new System.Windows.Forms.Timer(this.components); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.ID = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Group = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Server = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Enable = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.TotalConnect = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Connecting = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.AvgLatency = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.AvgDownSpeed = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.MaxDownSpeed = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.AvgUpSpeed = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.MaxUpSpeed = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Download = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Upload = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.DownloadRaw = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ErrorPercent = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ConnectError = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ConnectTimeout = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ConnectEmpty = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Continuous = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.ServerDataGrid)).BeginInit(); this.tableLayoutPanel1.SuspendLayout(); this.SuspendLayout(); // // ServerDataGrid // this.ServerDataGrid.AllowUserToAddRows = false; this.ServerDataGrid.AllowUserToDeleteRows = false; this.ServerDataGrid.AllowUserToResizeRows = false; this.ServerDataGrid.BorderStyle = System.Windows.Forms.BorderStyle.None; this.ServerDataGrid.ColumnHeadersHeight = 46; this.ServerDataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.ID, this.Group, this.Server, this.Enable, this.TotalConnect, this.Connecting, this.AvgLatency, this.AvgDownSpeed, this.MaxDownSpeed, this.AvgUpSpeed, this.MaxUpSpeed, this.Download, this.Upload, this.DownloadRaw, this.ErrorPercent, this.ConnectError, this.ConnectTimeout, this.ConnectEmpty, this.Continuous}); this.ServerDataGrid.Dock = System.Windows.Forms.DockStyle.Fill; this.ServerDataGrid.Location = new System.Drawing.Point(0, 0); this.ServerDataGrid.Margin = new System.Windows.Forms.Padding(0); this.ServerDataGrid.MinimumSize = new System.Drawing.Size(1, 1); this.ServerDataGrid.MultiSelect = false; this.ServerDataGrid.Name = "ServerDataGrid"; this.ServerDataGrid.ReadOnly = true; this.ServerDataGrid.RowHeadersVisible = false; this.ServerDataGrid.RowTemplate.Height = 23; this.ServerDataGrid.Size = new System.Drawing.Size(132, 34); this.ServerDataGrid.TabIndex = 0; this.ServerDataGrid.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.ServerDataGrid_CellClick); this.ServerDataGrid.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.ServerDataGrid_CellDoubleClick); this.ServerDataGrid.ColumnWidthChanged += new System.Windows.Forms.DataGridViewColumnEventHandler(this.ServerDataGrid_ColumnWidthChanged); this.ServerDataGrid.SortCompare += new System.Windows.Forms.DataGridViewSortCompareEventHandler(this.ServerDataGrid_SortCompare); this.ServerDataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ServerDataGrid_MouseUp); // // timer // this.timer.Enabled = true; this.timer.Interval = 250; this.timer.Tick += new System.EventHandler(this.timer_Tick); // // tableLayoutPanel1 // this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel1.ColumnCount = 1; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.ServerDataGrid, 0, 0); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.FixedSize; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.Size = new System.Drawing.Size(132, 22); this.tableLayoutPanel1.TabIndex = 1; // // ID // dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.ID.DefaultCellStyle = dataGridViewCellStyle1; this.ID.HeaderText = "ID"; this.ID.MinimumWidth = 2; this.ID.Name = "ID"; this.ID.ReadOnly = true; this.ID.Width = 36; // // Group // this.Group.HeaderText = "Group"; this.Group.Name = "Group"; this.Group.ReadOnly = true; this.Group.Width = 60; // // Server // dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.Server.DefaultCellStyle = dataGridViewCellStyle2; this.Server.HeaderText = "Server"; this.Server.MinimumWidth = 2; this.Server.Name = "Server"; this.Server.ReadOnly = true; this.Server.Width = 88; // // Enable // this.Enable.HeaderText = "Enable"; this.Enable.MinimumWidth = 8; this.Enable.Name = "Enable"; this.Enable.ReadOnly = true; this.Enable.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.Enable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.Enable.Width = 24; // // TotalConnect // this.TotalConnect.HeaderText = "Total Connect"; this.TotalConnect.MinimumWidth = 2; this.TotalConnect.Name = "TotalConnect"; this.TotalConnect.ReadOnly = true; this.TotalConnect.Visible = false; this.TotalConnect.Width = 48; // // Connecting // dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.Connecting.DefaultCellStyle = dataGridViewCellStyle3; this.Connecting.HeaderText = "Connecting"; this.Connecting.MinimumWidth = 16; this.Connecting.Name = "Connecting"; this.Connecting.ReadOnly = true; this.Connecting.Width = 28; // // AvgLatency // dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.AvgLatency.DefaultCellStyle = dataGridViewCellStyle4; this.AvgLatency.HeaderText = "Latency"; this.AvgLatency.MinimumWidth = 36; this.AvgLatency.Name = "AvgLatency"; this.AvgLatency.ReadOnly = true; this.AvgLatency.Width = 48; // // AvgDownSpeed // dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.AvgDownSpeed.DefaultCellStyle = dataGridViewCellStyle5; this.AvgDownSpeed.HeaderText = "Avg DSpeed"; this.AvgDownSpeed.MinimumWidth = 60; this.AvgDownSpeed.Name = "AvgDownSpeed"; this.AvgDownSpeed.ReadOnly = true; this.AvgDownSpeed.Width = 60; // // MaxDownSpeed // dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.MaxDownSpeed.DefaultCellStyle = dataGridViewCellStyle6; this.MaxDownSpeed.HeaderText = "Max DSpeed"; this.MaxDownSpeed.MinimumWidth = 2; this.MaxDownSpeed.Name = "MaxDownSpeed"; this.MaxDownSpeed.ReadOnly = true; this.MaxDownSpeed.Width = 60; // // AvgUpSpeed // dataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.AvgUpSpeed.DefaultCellStyle = dataGridViewCellStyle7; this.AvgUpSpeed.HeaderText = "Avg UpSpeed"; this.AvgUpSpeed.MinimumWidth = 60; this.AvgUpSpeed.Name = "AvgUpSpeed"; this.AvgUpSpeed.ReadOnly = true; this.AvgUpSpeed.Width = 60; // // MaxUpSpeed // dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.MaxUpSpeed.DefaultCellStyle = dataGridViewCellStyle8; this.MaxUpSpeed.HeaderText = "Max UpSpeed"; this.MaxUpSpeed.MinimumWidth = 2; this.MaxUpSpeed.Name = "MaxUpSpeed"; this.MaxUpSpeed.ReadOnly = true; this.MaxUpSpeed.Width = 60; // // Download // dataGridViewCellStyle9.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.Download.DefaultCellStyle = dataGridViewCellStyle9; this.Download.HeaderText = "Dload"; this.Download.MinimumWidth = 2; this.Download.Name = "Download"; this.Download.ReadOnly = true; this.Download.Width = 60; // // Upload // dataGridViewCellStyle10.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.Upload.DefaultCellStyle = dataGridViewCellStyle10; this.Upload.HeaderText = "Upload"; this.Upload.MinimumWidth = 2; this.Upload.Name = "Upload"; this.Upload.ReadOnly = true; this.Upload.Width = 60; // // DownloadRaw // dataGridViewCellStyle11.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.DownloadRaw.DefaultCellStyle = dataGridViewCellStyle11; this.DownloadRaw.HeaderText = "DloadRaw"; this.DownloadRaw.MinimumWidth = 2; this.DownloadRaw.Name = "DownloadRaw"; this.DownloadRaw.ReadOnly = true; this.DownloadRaw.Width = 60; // // ErrorPercent // this.ErrorPercent.HeaderText = "Error Percent"; this.ErrorPercent.MinimumWidth = 2; this.ErrorPercent.Name = "ErrorPercent"; this.ErrorPercent.ReadOnly = true; this.ErrorPercent.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.ErrorPercent.Visible = false; this.ErrorPercent.Width = 48; // // ConnectError // dataGridViewCellStyle12.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.ConnectError.DefaultCellStyle = dataGridViewCellStyle12; this.ConnectError.HeaderText = "Error"; this.ConnectError.MinimumWidth = 2; this.ConnectError.Name = "ConnectError"; this.ConnectError.ReadOnly = true; this.ConnectError.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.ConnectError.Width = 28; // // ConnectTimeout // dataGridViewCellStyle13.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.ConnectTimeout.DefaultCellStyle = dataGridViewCellStyle13; this.ConnectTimeout.HeaderText = "Timeout"; this.ConnectTimeout.MinimumWidth = 2; this.ConnectTimeout.Name = "ConnectTimeout"; this.ConnectTimeout.ReadOnly = true; this.ConnectTimeout.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.ConnectTimeout.Width = 28; // // ConnectEmpty // dataGridViewCellStyle14.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.ConnectEmpty.DefaultCellStyle = dataGridViewCellStyle14; this.ConnectEmpty.HeaderText = "Empty Response"; this.ConnectEmpty.MinimumWidth = 2; this.ConnectEmpty.Name = "ConnectEmpty"; this.ConnectEmpty.ReadOnly = true; this.ConnectEmpty.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; this.ConnectEmpty.Width = 28; // // Continuous // dataGridViewCellStyle15.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight; this.Continuous.DefaultCellStyle = dataGridViewCellStyle15; this.Continuous.HeaderText = "Continuous"; this.Continuous.Name = "Continuous"; this.Continuous.ReadOnly = true; this.Continuous.Visible = false; this.Continuous.Width = 28; // // ServerLogForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(132, 22); this.Controls.Add(this.tableLayoutPanel1); this.Margin = new System.Windows.Forms.Padding(2); this.Name = "ServerLogForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ServerLog"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ServerLogForm_FormClosed); this.ResizeEnd += new System.EventHandler(this.ServerLogForm_ResizeEnd); this.Move += new System.EventHandler(this.ServerLogForm_Move); ((System.ComponentModel.ISupportInitialize)(this.ServerDataGrid)).EndInit(); this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Timer timer; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private DoubleBufferListView ServerDataGrid; private System.Windows.Forms.DataGridViewTextBoxColumn ID; private System.Windows.Forms.DataGridViewTextBoxColumn Group; private System.Windows.Forms.DataGridViewTextBoxColumn Server; private System.Windows.Forms.DataGridViewTextBoxColumn Enable; private System.Windows.Forms.DataGridViewTextBoxColumn TotalConnect; private System.Windows.Forms.DataGridViewTextBoxColumn Connecting; private System.Windows.Forms.DataGridViewTextBoxColumn AvgLatency; private System.Windows.Forms.DataGridViewTextBoxColumn AvgDownSpeed; private System.Windows.Forms.DataGridViewTextBoxColumn MaxDownSpeed; private System.Windows.Forms.DataGridViewTextBoxColumn AvgUpSpeed; private System.Windows.Forms.DataGridViewTextBoxColumn MaxUpSpeed; private System.Windows.Forms.DataGridViewTextBoxColumn Download; private System.Windows.Forms.DataGridViewTextBoxColumn Upload; private System.Windows.Forms.DataGridViewTextBoxColumn DownloadRaw; private System.Windows.Forms.DataGridViewTextBoxColumn ErrorPercent; private System.Windows.Forms.DataGridViewTextBoxColumn ConnectError; private System.Windows.Forms.DataGridViewTextBoxColumn ConnectTimeout; private System.Windows.Forms.DataGridViewTextBoxColumn ConnectEmpty; private System.Windows.Forms.DataGridViewTextBoxColumn Continuous; } } ================================================ FILE: shadowsocks-csharp/View/ServerLogForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; using System.Threading; namespace Shadowsocks.View { public partial class ServerLogForm : Form { class DoubleBufferListView : DataGridView { public DoubleBufferListView() { SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint , true); UpdateStyles(); } } private ShadowsocksController controller; //private ContextMenu contextMenu1; private MenuItem topmostItem; private MenuItem clearItem; private List listOrder = new List(); private int lastRefreshIndex = 0; private bool firstDispley = true; private bool rowChange = false; private int updatePause = 0; private int updateTick = 0; private int updateSize = 0; private int pendingUpdate = 0; private string title_perfix = ""; private ServerSpeedLogShow[] ServerSpeedLogList; private Thread workerThread; private AutoResetEvent workerEvent = new AutoResetEvent(false); public ServerLogForm(ShadowsocksController controller) { this.controller = controller; try { this.Icon = Icon.FromHandle((new Bitmap("icon.png")).GetHicon()); title_perfix = System.Windows.Forms.Application.StartupPath; if (title_perfix.Length > 20) title_perfix = title_perfix.Substring(0, 20); } catch { this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); } this.Font = System.Drawing.SystemFonts.MessageBoxFont; InitializeComponent(); this.Width = 810; int dpi_mul = Util.Utils.GetDpiMul(); Configuration config = controller.GetCurrentConfiguration(); if (config.configs.Count < 8) { this.Height = 300 * dpi_mul / 4; } else if (config.configs.Count < 20) { this.Height = (300 + (config.configs.Count - 8) * 16) * dpi_mul / 4; } else { this.Height = 500 * dpi_mul / 4; } UpdateTexts(); UpdateLog(); this.Menu = new MainMenu(new MenuItem[] { CreateMenuGroup("&Control", new MenuItem[] { CreateMenuItem("&Disconnect direct connections", new EventHandler(this.DisconnectForward_Click)), CreateMenuItem("Disconnect &All", new EventHandler(this.Disconnect_Click)), new MenuItem("-"), CreateMenuItem("Clear &MaxSpeed", new EventHandler(this.ClearMaxSpeed_Click)), clearItem = CreateMenuItem("&Clear", new EventHandler(this.ClearItem_Click)), new MenuItem("-"), CreateMenuItem("Clear &Selected Total", new EventHandler(this.ClearSelectedTotal_Click)), CreateMenuItem("Clear &Total", new EventHandler(this.ClearTotal_Click)), }), CreateMenuGroup("Port &out", new MenuItem[] { CreateMenuItem("Copy current link", new EventHandler(this.copyLinkItem_Click)), CreateMenuItem("Copy current group links", new EventHandler(this.copyGroupLinkItem_Click)), CreateMenuItem("Copy all enable links", new EventHandler(this.copyEnableLinksItem_Click)), CreateMenuItem("Copy all links", new EventHandler(this.copyLinksItem_Click)), }), CreateMenuGroup("&Window", new MenuItem[] { CreateMenuItem("Auto &size", new EventHandler(this.autosizeItem_Click)), this.topmostItem = CreateMenuItem("Always On &Top", new EventHandler(this.topmostItem_Click)), }), }); controller.ConfigChanged += controller_ConfigChanged; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { ServerDataGrid.Columns[i].Width = ServerDataGrid.Columns[i].Width * dpi_mul / 4; } ServerDataGrid.RowTemplate.Height = 20 * dpi_mul / 4; //ServerDataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) continue; width += ServerDataGrid.Columns[i].Width; } this.Width = width + SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.AutoResizeColumnHeadersHeight(); } private MenuItem CreateMenuGroup(string text, MenuItem[] items) { return new MenuItem(I18N.GetString(text), items); } private MenuItem CreateMenuItem(string text, EventHandler click) { return new MenuItem(I18N.GetString(text), click); } private void UpdateTitle() { this.Text = title_perfix + I18N.GetString("ServerLog") + "(" + (controller.GetCurrentConfiguration().shareOverLan ? "any" : "local") + ":" + controller.GetCurrentConfiguration().localPort.ToString() + "(" + Model.Server.GetForwardServerRef().GetConnections().Count.ToString()+ ")" + " " + I18N.GetString("Version") + UpdateChecker.FullVersion + ")"; } private void UpdateTexts() { UpdateTitle(); for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { ServerDataGrid.Columns[i].HeaderText = I18N.GetString(ServerDataGrid.Columns[i].HeaderText); } } private void controller_ConfigChanged(object sender, EventArgs e) { UpdateTitle(); } private string FormatBytes(long bytes) { const long K = 1024L; const long M = K * 1024L; const long G = M * 1024L; const long T = G * 1024L; const long P = T * 1024L; const long E = P * 1024L; if (bytes >= M * 990) { if (bytes >= G * 990) { if (bytes >= P * 990) return (bytes / (double)E).ToString("F3") + "E"; if (bytes >= T * 990) return (bytes / (double)P).ToString("F3") + "P"; return (bytes / (double)T).ToString("F3") + "T"; } else { if (bytes >= G * 99) return (bytes / (double)G).ToString("F2") + "G"; if (bytes >= G * 9) return (bytes / (double)G).ToString("F3") + "G"; return (bytes / (double)G).ToString("F4") + "G"; } } else { if (bytes >= K * 990) { if (bytes >= M * 100) return (bytes / (double)M).ToString("F1") + "M"; if (bytes > M * 9.9) return (bytes / (double)M).ToString("F2") + "M"; return (bytes / (double)M).ToString("F3") + "M"; } else { if (bytes > K * 99) return (bytes / (double)K).ToString("F0") + "K"; if (bytes > 900) return (bytes / (double)K).ToString("F1") + "K"; return bytes.ToString(); } } } public bool SetBackColor(DataGridViewCell cell, Color newColor) { if (cell.Style.BackColor != newColor) { cell.Style.BackColor = newColor; rowChange = true; return true; } return false; } public bool SetCellToolTipText(DataGridViewCell cell, string newString) { if (cell.ToolTipText != newString) { cell.ToolTipText = newString; rowChange = true; return true; } return false; } public bool SetCellText(DataGridViewCell cell, string newString) { if ((string)cell.Value != newString) { cell.Value = newString; rowChange = true; return true; } return false; } public bool SetCellText(DataGridViewCell cell, long newInteger) { if ((string)cell.Value != newInteger.ToString()) { cell.Value = newInteger.ToString(); rowChange = true; return true; } return false; } byte ColorMix(byte a, byte b, double alpha) { return (byte)(b * alpha + a * (1 - alpha)); } Color ColorMix(Color a, Color b, double alpha) { return Color.FromArgb(ColorMix(a.R, b.R, alpha), ColorMix(a.G, b.G, alpha), ColorMix(a.B, b.B, alpha)); } public void UpdateLogThread() { while (workerThread != null) { Configuration config = controller.GetCurrentConfiguration(); ServerSpeedLogShow[] _ServerSpeedLogList = new ServerSpeedLogShow[config.configs.Count]; for (int i = 0; i < config.configs.Count; ++i) { _ServerSpeedLogList[i] = config.configs[i].ServerSpeedLog().Translate(); } ServerSpeedLogList = _ServerSpeedLogList; workerEvent.WaitOne(); } } public void UpdateLog() { if (workerThread == null) { workerThread = new Thread(this.UpdateLogThread); workerThread.Start(); } else { workerEvent.Set(); } } public void RefreshLog() { if (ServerSpeedLogList == null) return; int last_rowcount = ServerDataGrid.RowCount; Configuration config = controller.GetCurrentConfiguration(); if (listOrder.Count > config.configs.Count) { listOrder.RemoveRange(config.configs.Count, listOrder.Count - config.configs.Count); } while (listOrder.Count < config.configs.Count) { listOrder.Add(0); } while (ServerDataGrid.RowCount < config.configs.Count && ServerDataGrid.RowCount < ServerSpeedLogList.Length) { ServerDataGrid.Rows.Add(); int id = ServerDataGrid.RowCount - 1; ServerDataGrid[0, id].Value = id; } if (ServerDataGrid.RowCount > config.configs.Count) { for (int list_index = 0; list_index < ServerDataGrid.RowCount; ++list_index) { DataGridViewCell id_cell = ServerDataGrid[0, list_index]; int id = (int)id_cell.Value; if (id >= config.configs.Count) { ServerDataGrid.Rows.RemoveAt(list_index); --list_index; } } } int displayBeginIndex = ServerDataGrid.FirstDisplayedScrollingRowIndex; int displayEndIndex = displayBeginIndex + ServerDataGrid.DisplayedRowCount(true); try { for (int list_index = (lastRefreshIndex >= ServerDataGrid.RowCount) ? 0 : lastRefreshIndex, rowChangeCnt = 0; list_index < ServerDataGrid.RowCount && rowChangeCnt <= 100; ++list_index) { lastRefreshIndex = list_index + 1; DataGridViewCell id_cell = ServerDataGrid[0, list_index]; int id = (int)id_cell.Value; Server server = config.configs[id]; ServerSpeedLogShow serverSpeedLog = ServerSpeedLogList[id]; listOrder[id] = list_index; rowChange = false; for (int curcol = 0; curcol < ServerDataGrid.Columns.Count; ++curcol) { if (!firstDispley && (ServerDataGrid.SortedColumn == null || ServerDataGrid.SortedColumn.Index != curcol) && (list_index < displayBeginIndex || list_index >= displayEndIndex)) continue; DataGridViewCell cell = ServerDataGrid[curcol, list_index]; string columnName = ServerDataGrid.Columns[curcol].Name; // Server if (columnName == "Server") { if (config.index == id) SetBackColor(cell, Color.Cyan); else SetBackColor(cell, Color.White); SetCellText(cell, server.FriendlyName()); } if (columnName == "Group") { SetCellText(cell, server.group); } // Enable if (columnName == "Enable") { if (server.isEnable()) SetBackColor(cell, Color.White); else SetBackColor(cell, Color.Red); } // TotalConnectTimes else if (columnName == "TotalConnect") { SetCellText(cell, serverSpeedLog.totalConnectTimes); } // TotalConnecting else if (columnName == "Connecting") { long connections = serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes; //long ref_connections = server.GetConnections().Count; //if (ref_connections < connections) //{ // connections = ref_connections; //} Color[] colList = new Color[5] { Color.White, Color.LightGreen, Color.Yellow, Color.Red, Color.Red }; long[] bytesList = new long[5] { 0, 16, 32, 64, 65536 }; for (int i = 1; i < colList.Length; ++i) { if (connections < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(connections - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes); } // AvgConnectTime else if (columnName == "AvgLatency") { if (serverSpeedLog.avgConnectTime >= 0) SetCellText(cell, serverSpeedLog.avgConnectTime / 1000); else SetCellText(cell, "-"); } // AvgDownSpeed else if (columnName == "AvgDownSpeed") { long avgBytes = serverSpeedLog.avgDownloadBytes; string valStr = FormatBytes(avgBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (avgBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // MaxDownSpeed else if (columnName == "MaxDownSpeed") { long maxBytes = serverSpeedLog.maxDownloadBytes; string valStr = FormatBytes(maxBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (maxBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // AvgUpSpeed else if (columnName == "AvgUpSpeed") { long avgBytes = serverSpeedLog.avgUploadBytes; string valStr = FormatBytes(avgBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (avgBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // MaxUpSpeed else if (columnName == "MaxUpSpeed") { long maxBytes = serverSpeedLog.maxUploadBytes; string valStr = FormatBytes(maxBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (maxBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // TotalUploadBytes else if (columnName == "Upload") { string valStr = FormatBytes(serverSpeedLog.totalUploadBytes); string fullVal = serverSpeedLog.totalUploadBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4)); else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4)); //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } // TotalDownloadBytes else if (columnName == "Download") { string valStr = FormatBytes(serverSpeedLog.totalDownloadBytes); string fullVal = serverSpeedLog.totalDownloadBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0)); else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0)); //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } else if (columnName == "DownloadRaw") { string valStr = FormatBytes(serverSpeedLog.totalDownloadRawBytes); string fullVal = serverSpeedLog.totalDownloadRawBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") SetBackColor(cell, Color.FromArgb(0xff, 0x80, 0x80)); else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) { if (fullVal == "0") SetBackColor(cell, Color.FromArgb(0xff, 0x80, 0x80)); else SetBackColor(cell, Color.FromArgb(0xf0, 0xf0, 0xff)); } //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } // ErrorConnectTimes else if (columnName == "ConnectError") { long val = serverSpeedLog.errorConnectTimes + serverSpeedLog.errorDecodeTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 2.5), (byte)Math.Max(0, 255 - val * 2.5)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorTimeoutTimes else if (columnName == "ConnectTimeout") { SetCellText(cell, serverSpeedLog.errorTimeoutTimes); } // ErrorTimeoutTimes else if (columnName == "ConnectEmpty") { long val = serverSpeedLog.errorEmptyTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorContinurousTimes else if (columnName == "Continuous") { long val = serverSpeedLog.errorContinurousTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorPersent else if (columnName == "ErrorPercent") { if (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes > 0) { double percent = (serverSpeedLog.errorConnectTimes + serverSpeedLog.errorTimeoutTimes + serverSpeedLog.errorDecodeTimes) * 100.00 / (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes); SetBackColor(cell, Color.FromArgb(255, (byte)(255 - percent * 2), (byte)(255 - percent * 2))); SetCellText(cell, percent.ToString("F0") + "%"); } else { SetBackColor(cell, Color.White); SetCellText(cell, "-"); } } } if (rowChange && list_index >= displayBeginIndex && list_index < displayEndIndex) rowChangeCnt++; } } catch { } UpdateTitle(); if (ServerDataGrid.SortedColumn != null) { ServerDataGrid.Sort(ServerDataGrid.SortedColumn, (ListSortDirection)((int)ServerDataGrid.SortOrder - 1)); } if (last_rowcount == 0 && config.index >= 0 && config.index < ServerDataGrid.RowCount) { ServerDataGrid[0, config.index].Selected = true; } if (firstDispley) { ServerDataGrid.FirstDisplayedScrollingRowIndex = Math.Max(0, config.index - ServerDataGrid.DisplayedRowCount(true) / 2); firstDispley = false; } } private void autosizeColumns() { for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { string name = ServerDataGrid.Columns[i].Name; if (name == "AvgLatency" || name == "AvgDownSpeed" || name == "MaxDownSpeed" || name == "AvgUpSpeed" || name == "MaxUpSpeed" || name == "Upload" || name == "Download" || name == "DownloadRaw" || name == "Group" || name == "Connecting" || name == "ErrorPercent" || name == "ConnectError" || name == "ConnectTimeout" || name == "Continuous" || name == "ConnectEmpty" ) { if (ServerDataGrid.Columns[i].Width <= 2) continue; ServerDataGrid.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader); if (name == "AvgLatency" || name == "Connecting" || name == "AvgDownSpeed" || name == "MaxDownSpeed" || name == "AvgUpSpeed" || name == "MaxUpSpeed" ) { ServerDataGrid.Columns[i].MinimumWidth = ServerDataGrid.Columns[i].Width; } } } int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) continue; width += ServerDataGrid.Columns[i].Width; } this.Width = width + SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.AutoResizeColumnHeadersHeight(); } private void autosizeItem_Click(object sender, EventArgs e) { autosizeColumns(); } private void copyLinkItem_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); if (config.index >= 0 && config.index < config.configs.Count) { try { string link = config.configs[config.index].GetSSRLinkForServer(); Clipboard.SetText(link); } catch { } } } private void copyGroupLinkItem_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); if (config.index >= 0 && config.index < config.configs.Count) { string group = config.configs[config.index].group; string link = ""; for (int index = 0; index < config.configs.Count; ++index) { if (config.configs[index].group != group) continue; link += config.configs[index].GetSSRLinkForServer() + "\r\n"; } try { Clipboard.SetText(link); } catch { } } } private void copyEnableLinksItem_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); string link = ""; for (int index = 0; index < config.configs.Count; ++index) { if (!config.configs[index].enable) continue; link += config.configs[index].GetSSRLinkForServer() + "\r\n"; } try { Clipboard.SetText(link); } catch { } } private void copyLinksItem_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); string link = ""; for (int index = 0; index < config.configs.Count; ++index) { link += config.configs[index].GetSSRLinkForServer() + "\r\n"; } try { Clipboard.SetText(link); } catch { } } private void topmostItem_Click(object sender, EventArgs e) { topmostItem.Checked = !topmostItem.Checked; this.TopMost = topmostItem.Checked; } private void DisconnectForward_Click(object sender, EventArgs e) { Model.Server.GetForwardServerRef().GetConnections().CloseAll(); } private void Disconnect_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); for (int id = 0; id < config.configs.Count; ++id) { Server server = config.configs[id]; server.GetConnections().CloseAll(); } Model.Server.GetForwardServerRef().GetConnections().CloseAll(); } private void ClearMaxSpeed_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); foreach (Server server in config.configs) { server.ServerSpeedLog().ClearMaxSpeed(); } } private void ClearSelectedTotal_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); if (config.index >= 0 && config.index < config.configs.Count) { try { controller.ClearTransferTotal(config.configs[config.index].server); } catch { } } } private void ClearTotal_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); foreach (Server server in config.configs) { controller.ClearTransferTotal(server.server); } } private void ClearItem_Click(object sender, EventArgs e) { Configuration config = controller.GetCurrentConfiguration(); foreach (Server server in config.configs) { server.ServerSpeedLog().Clear(); } } private void timer_Tick(object sender, EventArgs e) { if (updatePause > 0) { updatePause -= 1; return; } if (this.WindowState == FormWindowState.Minimized) { if (++pendingUpdate < 40) { return; } } else { ++updateTick; } pendingUpdate = 0; RefreshLog(); UpdateLog(); if (updateSize > 1) --updateSize; if (updateTick == 2 || updateSize == 1) { updateSize = 0; //autosizeColumns(); } } private void ServerDataGrid_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { } else if (e.Button == MouseButtons.Left) { int row_index = -1, col_index = -1; if (ServerDataGrid.SelectedCells.Count > 0) { row_index = ServerDataGrid.SelectedCells[0].RowIndex; col_index = ServerDataGrid.SelectedCells[0].ColumnIndex; } if (row_index >= 0) { int id = (int)ServerDataGrid[0, row_index].Value; if (ServerDataGrid.Columns[col_index].Name == "Server") { Configuration config = controller.GetCurrentConfiguration(); controller.SelectServerIndex(id); } if (ServerDataGrid.Columns[col_index].Name == "Group") { Configuration config = controller.GetCurrentConfiguration(); Server cur_server = config.configs[id]; string group = cur_server.group; if (!string.IsNullOrEmpty(group)) { bool enable = !cur_server.enable; foreach (Server server in config.configs) { if (server.group == group) { if (server.enable != enable) { server.setEnable(enable); } } } controller.SelectServerIndex(config.index); } } if (ServerDataGrid.Columns[col_index].Name == "Enable") { Configuration config = controller.GetCurrentConfiguration(); Server server = config.configs[id]; server.setEnable(!server.isEnable()); controller.SelectServerIndex(config.index); } ServerDataGrid[0, row_index].Selected = true; } } } private void ServerDataGrid_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { int id = (int)ServerDataGrid[0, e.RowIndex].Value; if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Server") { Configuration config = controller.GetCurrentConfiguration(); controller.SelectServerIndex(id); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Group") { Configuration config = controller.GetCurrentConfiguration(); Server cur_server = config.configs[id]; string group = cur_server.group; if (!string.IsNullOrEmpty(group)) { bool enable = !cur_server.enable; foreach (Server server in config.configs) { if (server.group == group) { if (server.enable != enable) { server.setEnable(enable); } } } controller.SelectServerIndex(config.index); } } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Enable") { Configuration config = controller.GetCurrentConfiguration(); Server server = config.configs[id]; server.setEnable(!server.isEnable()); controller.SelectServerIndex(config.index); } ServerDataGrid[0, e.RowIndex].Selected = true; } } private void ServerDataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { int id = (int)ServerDataGrid[0, e.RowIndex].Value; if (ServerDataGrid.Columns[e.ColumnIndex].Name == "ID") { controller.ShowConfigForm(id); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Server") { controller.ShowConfigForm(id); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Connecting") { Configuration config = controller.GetCurrentConfiguration(); Server server = config.configs[id]; server.GetConnections().CloseAll(); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "MaxDownSpeed" || ServerDataGrid.Columns[e.ColumnIndex].Name == "MaxUpSpeed") { Configuration config = controller.GetCurrentConfiguration(); config.configs[id].ServerSpeedLog().ClearMaxSpeed(); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "Upload" || ServerDataGrid.Columns[e.ColumnIndex].Name == "Download") { Configuration config = controller.GetCurrentConfiguration(); config.configs[id].ServerSpeedLog().ClearTrans(); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "DownloadRaw") { Configuration config = controller.GetCurrentConfiguration(); config.configs[id].ServerSpeedLog().Clear(); config.configs[id].setEnable(true); } if (ServerDataGrid.Columns[e.ColumnIndex].Name == "ConnectError" || ServerDataGrid.Columns[e.ColumnIndex].Name == "ConnectTimeout" || ServerDataGrid.Columns[e.ColumnIndex].Name == "ConnectEmpty" || ServerDataGrid.Columns[e.ColumnIndex].Name == "Continuous" ) { Configuration config = controller.GetCurrentConfiguration(); config.configs[id].ServerSpeedLog().ClearError(); config.configs[id].setEnable(true); } ServerDataGrid[0, e.RowIndex].Selected = true; } } private void ServerLogForm_FormClosed(object sender, FormClosedEventArgs e) { controller.ConfigChanged -= controller_ConfigChanged; Thread thread = workerThread; workerThread = null; while (thread.IsAlive) { workerEvent.Set(); Thread.Sleep(50); } } private long Str2Long(String str) { if (str == "-") return -1; //if (String.IsNullOrEmpty(str)) return -1; if (str.LastIndexOf('K') > 0) { Double ret = Convert.ToDouble(str.Substring(0, str.LastIndexOf('K'))); return (long)(ret * 1024); } if (str.LastIndexOf('M') > 0) { Double ret = Convert.ToDouble(str.Substring(0, str.LastIndexOf('M'))); return (long)(ret * 1024 * 1024); } if (str.LastIndexOf('G') > 0) { Double ret = Convert.ToDouble(str.Substring(0, str.LastIndexOf('G'))); return (long)(ret * 1024 * 1024 * 1024); } if (str.LastIndexOf('T') > 0) { Double ret = Convert.ToDouble(str.Substring(0, str.LastIndexOf('T'))); return (long)(ret * 1024 * 1024 * 1024 * 1024); } try { Double ret = Convert.ToDouble(str); return (long)ret; } catch { return -1; } } private void ServerDataGrid_SortCompare(object sender, DataGridViewSortCompareEventArgs e) { //e.SortResult = 0; if (e.Column.Name == "Server" || e.Column.Name == "Group") { e.SortResult = System.String.Compare(Convert.ToString(e.CellValue1), Convert.ToString(e.CellValue2)); e.Handled = true; } else if (e.Column.Name == "ID" || e.Column.Name == "TotalConnect" || e.Column.Name == "Connecting" || e.Column.Name == "ConnectError" || e.Column.Name == "ConnectTimeout" || e.Column.Name == "Continuous" ) { Int32 v1 = Convert.ToInt32(e.CellValue1); Int32 v2 = Convert.ToInt32(e.CellValue2); e.SortResult = (v1 == v2 ? 0 : (v1 < v2 ? -1 : 1)); } else if (e.Column.Name == "ErrorPercent") { String s1 = Convert.ToString(e.CellValue1); String s2 = Convert.ToString(e.CellValue2); Int32 v1 = s1.Length <= 1 ? 0 : Convert.ToInt32(Convert.ToDouble(s1.Substring(0, s1.Length - 1)) * 100); Int32 v2 = s2.Length <= 1 ? 0 : Convert.ToInt32(Convert.ToDouble(s2.Substring(0, s2.Length - 1)) * 100); e.SortResult = v1 == v2 ? 0 : v1 < v2 ? -1 : 1; } else if (e.Column.Name == "AvgLatency" || e.Column.Name == "AvgDownSpeed" || e.Column.Name == "MaxDownSpeed" || e.Column.Name == "AvgUpSpeed" || e.Column.Name == "MaxUpSpeed" || e.Column.Name == "Upload" || e.Column.Name == "Download" || e.Column.Name == "DownloadRaw" ) { String s1 = Convert.ToString(e.CellValue1); String s2 = Convert.ToString(e.CellValue2); long v1 = Str2Long(s1); long v2 = Str2Long(s2); e.SortResult = (v1 == v2 ? 0 : (v1 < v2 ? -1 : 1)); } if (e.SortResult == 0) { int v1 = listOrder[Convert.ToInt32(ServerDataGrid[0, e.RowIndex1].Value)]; int v2 = listOrder[Convert.ToInt32(ServerDataGrid[0, e.RowIndex2].Value)]; e.SortResult = (v1 == v2 ? 0 : (v1 < v2 ? -1 : 1)); if (e.SortResult != 0 && ServerDataGrid.SortOrder == SortOrder.Descending) { e.SortResult = -e.SortResult; } } if (e.SortResult != 0) { e.Handled = true; } } private void ServerLogForm_Move(object sender, EventArgs e) { updatePause = 0; } protected override void WndProc(ref Message message) { const int WM_SIZING = 532; //const int WM_SIZE = 533; const int WM_MOVING = 534; const int WM_SYSCOMMAND = 0x0112; const int SC_MINIMIZE = 0xF020; switch (message.Msg) { case WM_SIZING: case WM_MOVING: updatePause = 2; break; case WM_SYSCOMMAND: if ((int)message.WParam == SC_MINIMIZE) { Util.Utils.ReleaseMemory(); } break; } base.WndProc(ref message); } private void ServerLogForm_ResizeEnd(object sender, EventArgs e) { updatePause = 0; int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) continue; width += ServerDataGrid.Columns[i].Width; } width += SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.Columns[2].Width += this.Width - width; } private void ServerDataGrid_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) { int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) continue; width += ServerDataGrid.Columns[i].Width; } this.Width = width + SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.AutoResizeColumnHeadersHeight(); } } } ================================================ FILE: shadowsocks-csharp/View/ServerLogForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 True True True True True True True True True True True True True True True True True True True 17, 17 ================================================ FILE: shadowsocks-csharp/View/SettingsForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class SettingsForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.LabelRandom = new System.Windows.Forms.Label(); this.RandomComboBox = new System.Windows.Forms.ComboBox(); this.CheckAutoBan = new System.Windows.Forms.CheckBox(); this.checkRandom = new System.Windows.Forms.CheckBox(); this.checkAutoStartup = new System.Windows.Forms.CheckBox(); this.checkBalanceInGroup = new System.Windows.Forms.CheckBox(); this.Socks5ProxyGroup = new System.Windows.Forms.GroupBox(); this.tableLayoutPanel9 = new System.Windows.Forms.TableLayoutPanel(); this.LabelS5Password = new System.Windows.Forms.Label(); this.LabelS5Username = new System.Windows.Forms.Label(); this.TextS5Pass = new System.Windows.Forms.TextBox(); this.LabelS5Port = new System.Windows.Forms.Label(); this.TextS5User = new System.Windows.Forms.TextBox(); this.LabelS5Server = new System.Windows.Forms.Label(); this.NumS5Port = new System.Windows.Forms.NumericUpDown(); this.TextS5Server = new System.Windows.Forms.TextBox(); this.comboProxyType = new System.Windows.Forms.ComboBox(); this.CheckSockProxy = new System.Windows.Forms.CheckBox(); this.checkBoxPacProxy = new System.Windows.Forms.CheckBox(); this.label1 = new System.Windows.Forms.Label(); this.TextUserAgent = new System.Windows.Forms.TextBox(); this.ListenGroup = new System.Windows.Forms.GroupBox(); this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); this.TextAuthPass = new System.Windows.Forms.TextBox(); this.LabelAuthPass = new System.Windows.Forms.Label(); this.TextAuthUser = new System.Windows.Forms.TextBox(); this.LabelAuthUser = new System.Windows.Forms.Label(); this.checkShareOverLan = new System.Windows.Forms.CheckBox(); this.NumProxyPort = new System.Windows.Forms.NumericUpDown(); this.ProxyPortLabel = new System.Windows.Forms.Label(); this.tableLayoutPanel10 = new System.Windows.Forms.TableLayoutPanel(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.MyCancelButton = new System.Windows.Forms.Button(); this.OKButton = new System.Windows.Forms.Button(); this.tableLayoutPanel5 = new System.Windows.Forms.TableLayoutPanel(); this.ReconnectLabel = new System.Windows.Forms.Label(); this.NumReconnect = new System.Windows.Forms.NumericUpDown(); this.TTLLabel = new System.Windows.Forms.Label(); this.NumTTL = new System.Windows.Forms.NumericUpDown(); this.labelTimeout = new System.Windows.Forms.Label(); this.NumTimeout = new System.Windows.Forms.NumericUpDown(); this.DNSText = new System.Windows.Forms.TextBox(); this.buttonDefault = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.LocalDNSText = new System.Windows.Forms.TextBox(); this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); this.Socks5ProxyGroup.SuspendLayout(); this.tableLayoutPanel9.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumS5Port)).BeginInit(); this.ListenGroup.SuspendLayout(); this.tableLayoutPanel4.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumProxyPort)).BeginInit(); this.tableLayoutPanel10.SuspendLayout(); this.tableLayoutPanel3.SuspendLayout(); this.tableLayoutPanel5.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumReconnect)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NumTTL)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.NumTimeout)).BeginInit(); this.SuspendLayout(); // // tableLayoutPanel1 // this.tableLayoutPanel1.AutoSize = true; this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 1, 0); this.tableLayoutPanel1.Controls.Add(this.Socks5ProxyGroup, 0, 0); this.tableLayoutPanel1.Controls.Add(this.ListenGroup, 0, 2); this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel10, 1, 2); this.tableLayoutPanel1.Location = new System.Drawing.Point(15, 16); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 3; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.Size = new System.Drawing.Size(581, 479); this.tableLayoutPanel1.TabIndex = 0; // // tableLayoutPanel2 // this.tableLayoutPanel2.AutoSize = true; this.tableLayoutPanel2.ColumnCount = 2; this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel2.Controls.Add(this.LabelRandom, 0, 2); this.tableLayoutPanel2.Controls.Add(this.RandomComboBox, 1, 2); this.tableLayoutPanel2.Controls.Add(this.CheckAutoBan, 1, 4); this.tableLayoutPanel2.Controls.Add(this.checkRandom, 1, 1); this.tableLayoutPanel2.Controls.Add(this.checkAutoStartup, 1, 0); this.tableLayoutPanel2.Controls.Add(this.checkBalanceInGroup, 1, 3); this.tableLayoutPanel2.Location = new System.Drawing.Point(372, 3); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; this.tableLayoutPanel2.RowCount = 5; this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel2.Size = new System.Drawing.Size(206, 118); this.tableLayoutPanel2.TabIndex = 2; // // LabelRandom // this.LabelRandom.AutoSize = true; this.LabelRandom.Dock = System.Windows.Forms.DockStyle.Fill; this.LabelRandom.Location = new System.Drawing.Point(3, 44); this.LabelRandom.Name = "LabelRandom"; this.LabelRandom.Size = new System.Drawing.Size(47, 30); this.LabelRandom.TabIndex = 12; this.LabelRandom.Text = "Balance"; this.LabelRandom.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // RandomComboBox // this.RandomComboBox.Anchor = System.Windows.Forms.AnchorStyles.None; this.RandomComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.RandomComboBox.FormattingEnabled = true; this.RandomComboBox.Items.AddRange(new object[] { "OneByOne", "Random", "FastDownloadSpeed", "LowLatency", "LowException", "SelectedFirst", "Timer"}); this.RandomComboBox.Location = new System.Drawing.Point(56, 47); this.RandomComboBox.Margin = new System.Windows.Forms.Padding(3, 3, 3, 7); this.RandomComboBox.Name = "RandomComboBox"; this.RandomComboBox.Size = new System.Drawing.Size(147, 20); this.RandomComboBox.TabIndex = 14; // // CheckAutoBan // this.CheckAutoBan.AutoSize = true; this.CheckAutoBan.Enabled = false; this.CheckAutoBan.Location = new System.Drawing.Point(56, 99); this.CheckAutoBan.Name = "CheckAutoBan"; this.CheckAutoBan.Size = new System.Drawing.Size(66, 16); this.CheckAutoBan.TabIndex = 15; this.CheckAutoBan.Text = "AutoBan"; this.CheckAutoBan.UseVisualStyleBackColor = true; // // checkRandom // this.checkRandom.AutoSize = true; this.checkRandom.Location = new System.Drawing.Point(56, 25); this.checkRandom.Name = "checkRandom"; this.checkRandom.Size = new System.Drawing.Size(96, 16); this.checkRandom.TabIndex = 13; this.checkRandom.Text = "Load balance"; this.checkRandom.UseVisualStyleBackColor = true; // // checkAutoStartup // this.checkAutoStartup.AutoSize = true; this.checkAutoStartup.Location = new System.Drawing.Point(56, 3); this.checkAutoStartup.Name = "checkAutoStartup"; this.checkAutoStartup.Size = new System.Drawing.Size(102, 16); this.checkAutoStartup.TabIndex = 12; this.checkAutoStartup.Text = "Start on Boot"; this.checkAutoStartup.UseVisualStyleBackColor = true; // // checkBalanceInGroup // this.checkBalanceInGroup.AutoSize = true; this.checkBalanceInGroup.Location = new System.Drawing.Point(56, 77); this.checkBalanceInGroup.Name = "checkBalanceInGroup"; this.checkBalanceInGroup.Size = new System.Drawing.Size(120, 16); this.checkBalanceInGroup.TabIndex = 15; this.checkBalanceInGroup.Text = "Balance in group"; this.checkBalanceInGroup.UseVisualStyleBackColor = true; // // Socks5ProxyGroup // this.Socks5ProxyGroup.AutoSize = true; this.Socks5ProxyGroup.Controls.Add(this.tableLayoutPanel9); this.Socks5ProxyGroup.Location = new System.Drawing.Point(14, 0); this.Socks5ProxyGroup.Margin = new System.Windows.Forms.Padding(14, 0, 0, 0); this.Socks5ProxyGroup.Name = "Socks5ProxyGroup"; this.tableLayoutPanel1.SetRowSpan(this.Socks5ProxyGroup, 2); this.Socks5ProxyGroup.Size = new System.Drawing.Size(355, 255); this.Socks5ProxyGroup.TabIndex = 0; this.Socks5ProxyGroup.TabStop = false; this.Socks5ProxyGroup.Text = "Remote proxy"; // // tableLayoutPanel9 // this.tableLayoutPanel9.AutoSize = true; this.tableLayoutPanel9.ColumnCount = 2; this.tableLayoutPanel9.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel9.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel9.Controls.Add(this.LabelS5Password, 0, 5); this.tableLayoutPanel9.Controls.Add(this.LabelS5Username, 0, 4); this.tableLayoutPanel9.Controls.Add(this.TextS5Pass, 1, 5); this.tableLayoutPanel9.Controls.Add(this.LabelS5Port, 0, 3); this.tableLayoutPanel9.Controls.Add(this.TextS5User, 1, 4); this.tableLayoutPanel9.Controls.Add(this.LabelS5Server, 0, 2); this.tableLayoutPanel9.Controls.Add(this.NumS5Port, 1, 3); this.tableLayoutPanel9.Controls.Add(this.TextS5Server, 1, 2); this.tableLayoutPanel9.Controls.Add(this.comboProxyType, 1, 1); this.tableLayoutPanel9.Controls.Add(this.CheckSockProxy, 0, 0); this.tableLayoutPanel9.Controls.Add(this.checkBoxPacProxy, 1, 0); this.tableLayoutPanel9.Controls.Add(this.label1, 0, 6); this.tableLayoutPanel9.Controls.Add(this.TextUserAgent, 1, 6); this.tableLayoutPanel9.Location = new System.Drawing.Point(11, 32); this.tableLayoutPanel9.Name = "tableLayoutPanel9"; this.tableLayoutPanel9.RowCount = 7; this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel9.Size = new System.Drawing.Size(338, 203); this.tableLayoutPanel9.TabIndex = 0; // // LabelS5Password // this.LabelS5Password.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelS5Password.AutoSize = true; this.LabelS5Password.Location = new System.Drawing.Point(22, 136); this.LabelS5Password.Name = "LabelS5Password"; this.LabelS5Password.Size = new System.Drawing.Size(53, 12); this.LabelS5Password.TabIndex = 5; this.LabelS5Password.Text = "Password"; // // LabelS5Username // this.LabelS5Username.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelS5Username.AutoSize = true; this.LabelS5Username.Location = new System.Drawing.Point(22, 109); this.LabelS5Username.Name = "LabelS5Username"; this.LabelS5Username.Size = new System.Drawing.Size(53, 12); this.LabelS5Username.TabIndex = 4; this.LabelS5Username.Text = "Username"; // // TextS5Pass // this.TextS5Pass.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextS5Pass.Location = new System.Drawing.Point(81, 132); this.TextS5Pass.Name = "TextS5Pass"; this.TextS5Pass.Size = new System.Drawing.Size(236, 21); this.TextS5Pass.TabIndex = 6; // // LabelS5Port // this.LabelS5Port.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelS5Port.AutoSize = true; this.LabelS5Port.Location = new System.Drawing.Point(46, 82); this.LabelS5Port.Name = "LabelS5Port"; this.LabelS5Port.Size = new System.Drawing.Size(29, 12); this.LabelS5Port.TabIndex = 1; this.LabelS5Port.Text = "Port"; // // TextS5User // this.TextS5User.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextS5User.Location = new System.Drawing.Point(81, 105); this.TextS5User.Name = "TextS5User"; this.TextS5User.Size = new System.Drawing.Size(236, 21); this.TextS5User.TabIndex = 5; // // LabelS5Server // this.LabelS5Server.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelS5Server.AutoSize = true; this.LabelS5Server.Location = new System.Drawing.Point(16, 55); this.LabelS5Server.Name = "LabelS5Server"; this.LabelS5Server.Size = new System.Drawing.Size(59, 12); this.LabelS5Server.TabIndex = 0; this.LabelS5Server.Text = "Server IP"; // // NumS5Port // this.NumS5Port.ImeMode = System.Windows.Forms.ImeMode.Off; this.NumS5Port.Location = new System.Drawing.Point(81, 78); this.NumS5Port.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumS5Port.Name = "NumS5Port"; this.NumS5Port.Size = new System.Drawing.Size(236, 21); this.NumS5Port.TabIndex = 4; // // TextS5Server // this.TextS5Server.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextS5Server.Location = new System.Drawing.Point(81, 51); this.TextS5Server.Name = "TextS5Server"; this.TextS5Server.Size = new System.Drawing.Size(236, 21); this.TextS5Server.TabIndex = 3; // // comboProxyType // this.comboProxyType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboProxyType.FormattingEnabled = true; this.comboProxyType.Items.AddRange(new object[] { "Socks5(support UDP)", "Http tunnel", "TCP Port tunnel"}); this.comboProxyType.Location = new System.Drawing.Point(81, 25); this.comboProxyType.Name = "comboProxyType"; this.comboProxyType.Size = new System.Drawing.Size(236, 20); this.comboProxyType.TabIndex = 2; // // CheckSockProxy // this.CheckSockProxy.AutoSize = true; this.CheckSockProxy.Location = new System.Drawing.Point(3, 3); this.CheckSockProxy.Name = "CheckSockProxy"; this.CheckSockProxy.Size = new System.Drawing.Size(72, 16); this.CheckSockProxy.TabIndex = 0; this.CheckSockProxy.Text = "Proxy On"; this.CheckSockProxy.UseVisualStyleBackColor = true; // // checkBoxPacProxy // this.checkBoxPacProxy.AutoSize = true; this.checkBoxPacProxy.Location = new System.Drawing.Point(81, 3); this.checkBoxPacProxy.Name = "checkBoxPacProxy"; this.checkBoxPacProxy.Size = new System.Drawing.Size(204, 16); this.checkBoxPacProxy.TabIndex = 1; this.checkBoxPacProxy.Text = "PAC \"direct\" return this proxy"; this.checkBoxPacProxy.UseVisualStyleBackColor = true; // // label1 // this.label1.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 173); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(59, 12); this.label1.TabIndex = 5; this.label1.Text = "UserAgent"; // // TextUserAgent // this.TextUserAgent.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextUserAgent.Location = new System.Drawing.Point(81, 159); this.TextUserAgent.Name = "TextUserAgent"; this.TextUserAgent.Size = new System.Drawing.Size(236, 21); this.TextUserAgent.TabIndex = 7; // // ListenGroup // this.ListenGroup.AutoSize = true; this.ListenGroup.Controls.Add(this.tableLayoutPanel4); this.ListenGroup.Location = new System.Drawing.Point(14, 255); this.ListenGroup.Margin = new System.Windows.Forms.Padding(14, 0, 0, 0); this.ListenGroup.Name = "ListenGroup"; this.ListenGroup.Size = new System.Drawing.Size(339, 176); this.ListenGroup.TabIndex = 1; this.ListenGroup.TabStop = false; this.ListenGroup.Text = "Local proxy"; // // tableLayoutPanel4 // this.tableLayoutPanel4.AutoSize = true; this.tableLayoutPanel4.ColumnCount = 2; this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel4.Controls.Add(this.TextAuthPass, 1, 3); this.tableLayoutPanel4.Controls.Add(this.LabelAuthPass, 0, 3); this.tableLayoutPanel4.Controls.Add(this.TextAuthUser, 1, 2); this.tableLayoutPanel4.Controls.Add(this.LabelAuthUser, 0, 2); this.tableLayoutPanel4.Controls.Add(this.checkShareOverLan, 1, 0); this.tableLayoutPanel4.Controls.Add(this.NumProxyPort, 1, 1); this.tableLayoutPanel4.Controls.Add(this.ProxyPortLabel, 0, 1); this.tableLayoutPanel4.Location = new System.Drawing.Point(5, 32); this.tableLayoutPanel4.Name = "tableLayoutPanel4"; this.tableLayoutPanel4.RowCount = 4; this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel4.Size = new System.Drawing.Size(328, 124); this.tableLayoutPanel4.TabIndex = 0; // // TextAuthPass // this.TextAuthPass.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextAuthPass.Location = new System.Drawing.Point(62, 79); this.TextAuthPass.Name = "TextAuthPass"; this.TextAuthPass.Size = new System.Drawing.Size(236, 21); this.TextAuthPass.TabIndex = 11; // // LabelAuthPass // this.LabelAuthPass.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelAuthPass.AutoSize = true; this.LabelAuthPass.Location = new System.Drawing.Point(3, 94); this.LabelAuthPass.Name = "LabelAuthPass"; this.LabelAuthPass.Size = new System.Drawing.Size(53, 12); this.LabelAuthPass.TabIndex = 8; this.LabelAuthPass.Text = "Password"; // // TextAuthUser // this.TextAuthUser.ImeMode = System.Windows.Forms.ImeMode.Off; this.TextAuthUser.Location = new System.Drawing.Point(62, 52); this.TextAuthUser.Name = "TextAuthUser"; this.TextAuthUser.Size = new System.Drawing.Size(236, 21); this.TextAuthUser.TabIndex = 10; // // LabelAuthUser // this.LabelAuthUser.Anchor = System.Windows.Forms.AnchorStyles.Right; this.LabelAuthUser.AutoSize = true; this.LabelAuthUser.Location = new System.Drawing.Point(3, 56); this.LabelAuthUser.Name = "LabelAuthUser"; this.LabelAuthUser.Size = new System.Drawing.Size(53, 12); this.LabelAuthUser.TabIndex = 5; this.LabelAuthUser.Text = "Username"; // // checkShareOverLan // this.checkShareOverLan.AutoSize = true; this.checkShareOverLan.Location = new System.Drawing.Point(62, 3); this.checkShareOverLan.Name = "checkShareOverLan"; this.checkShareOverLan.Size = new System.Drawing.Size(156, 16); this.checkShareOverLan.TabIndex = 8; this.checkShareOverLan.Text = "Allow Clients from LAN"; this.checkShareOverLan.UseVisualStyleBackColor = true; // // NumProxyPort // this.NumProxyPort.ImeMode = System.Windows.Forms.ImeMode.Off; this.NumProxyPort.Location = new System.Drawing.Point(62, 25); this.NumProxyPort.Maximum = new decimal(new int[] { 65535, 0, 0, 0}); this.NumProxyPort.Name = "NumProxyPort"; this.NumProxyPort.Size = new System.Drawing.Size(236, 21); this.NumProxyPort.TabIndex = 9; // // ProxyPortLabel // this.ProxyPortLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ProxyPortLabel.AutoSize = true; this.ProxyPortLabel.Location = new System.Drawing.Point(27, 29); this.ProxyPortLabel.Name = "ProxyPortLabel"; this.ProxyPortLabel.Size = new System.Drawing.Size(29, 12); this.ProxyPortLabel.TabIndex = 3; this.ProxyPortLabel.Text = "Port"; // // tableLayoutPanel10 // this.tableLayoutPanel10.Anchor = System.Windows.Forms.AnchorStyles.None; this.tableLayoutPanel10.AutoSize = true; this.tableLayoutPanel10.ColumnCount = 1; this.tableLayoutPanel10.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel10.Controls.Add(this.tableLayoutPanel3, 0, 2); this.tableLayoutPanel10.Controls.Add(this.tableLayoutPanel5, 0, 1); this.tableLayoutPanel10.Location = new System.Drawing.Point(382, 258); this.tableLayoutPanel10.Name = "tableLayoutPanel10"; this.tableLayoutPanel10.RowCount = 3; this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel10.Size = new System.Drawing.Size(186, 218); this.tableLayoutPanel10.TabIndex = 3; // // tableLayoutPanel3 // this.tableLayoutPanel3.Anchor = System.Windows.Forms.AnchorStyles.Bottom; this.tableLayoutPanel3.AutoSize = true; this.tableLayoutPanel3.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel3.ColumnCount = 2; this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 23F)); this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); this.tableLayoutPanel3.Location = new System.Drawing.Point(3, 173); this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); this.tableLayoutPanel3.Name = "tableLayoutPanel3"; this.tableLayoutPanel3.RowCount = 1; this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel3.Size = new System.Drawing.Size(183, 42); this.tableLayoutPanel3.TabIndex = 14; // // MyCancelButton // this.MyCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.MyCancelButton.Dock = System.Windows.Forms.DockStyle.Right; this.MyCancelButton.Location = new System.Drawing.Point(96, 3); this.MyCancelButton.Margin = new System.Windows.Forms.Padding(3, 3, 0, 0); this.MyCancelButton.Name = "MyCancelButton"; this.MyCancelButton.Size = new System.Drawing.Size(87, 39); this.MyCancelButton.TabIndex = 22; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; this.MyCancelButton.Click += new System.EventHandler(this.CancelButton_Click); // // OKButton // this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK; this.OKButton.Dock = System.Windows.Forms.DockStyle.Right; this.OKButton.Location = new System.Drawing.Point(3, 3); this.OKButton.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0); this.OKButton.Name = "OKButton"; this.OKButton.Size = new System.Drawing.Size(87, 39); this.OKButton.TabIndex = 21; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; this.OKButton.Click += new System.EventHandler(this.OKButton_Click); // // tableLayoutPanel5 // this.tableLayoutPanel5.Anchor = System.Windows.Forms.AnchorStyles.Bottom; this.tableLayoutPanel5.AutoSize = true; this.tableLayoutPanel5.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.tableLayoutPanel5.ColumnCount = 2; this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel5.Controls.Add(this.ReconnectLabel, 0, 4); this.tableLayoutPanel5.Controls.Add(this.NumReconnect, 1, 4); this.tableLayoutPanel5.Controls.Add(this.TTLLabel, 0, 6); this.tableLayoutPanel5.Controls.Add(this.NumTTL, 1, 6); this.tableLayoutPanel5.Controls.Add(this.labelTimeout, 0, 5); this.tableLayoutPanel5.Controls.Add(this.NumTimeout, 1, 5); this.tableLayoutPanel5.Controls.Add(this.DNSText, 1, 1); this.tableLayoutPanel5.Controls.Add(this.buttonDefault, 1, 0); this.tableLayoutPanel5.Controls.Add(this.label2, 0, 1); this.tableLayoutPanel5.Controls.Add(this.label3, 0, 2); this.tableLayoutPanel5.Controls.Add(this.LocalDNSText, 1, 2); this.tableLayoutPanel5.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel5.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel5.Name = "tableLayoutPanel5"; this.tableLayoutPanel5.Padding = new System.Windows.Forms.Padding(3); this.tableLayoutPanel5.RowCount = 7; this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.Size = new System.Drawing.Size(186, 170); this.tableLayoutPanel5.TabIndex = 3; // // ReconnectLabel // this.ReconnectLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ReconnectLabel.AutoSize = true; this.ReconnectLabel.Location = new System.Drawing.Point(6, 93); this.ReconnectLabel.Name = "ReconnectLabel"; this.ReconnectLabel.Size = new System.Drawing.Size(59, 12); this.ReconnectLabel.TabIndex = 3; this.ReconnectLabel.Text = "Reconnect"; // // NumReconnect // this.NumReconnect.ImeMode = System.Windows.Forms.ImeMode.Off; this.NumReconnect.Location = new System.Drawing.Point(71, 89); this.NumReconnect.Maximum = new decimal(new int[] { 20, 0, 0, 0}); this.NumReconnect.Name = "NumReconnect"; this.NumReconnect.Size = new System.Drawing.Size(109, 21); this.NumReconnect.TabIndex = 18; // // TTLLabel // this.TTLLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.TTLLabel.AutoSize = true; this.TTLLabel.Location = new System.Drawing.Point(42, 147); this.TTLLabel.Name = "TTLLabel"; this.TTLLabel.Size = new System.Drawing.Size(23, 12); this.TTLLabel.TabIndex = 3; this.TTLLabel.Text = "TTL"; // // NumTTL // this.NumTTL.ImeMode = System.Windows.Forms.ImeMode.Off; this.NumTTL.Location = new System.Drawing.Point(71, 143); this.NumTTL.Maximum = new decimal(new int[] { 600, 0, 0, 0}); this.NumTTL.Name = "NumTTL"; this.NumTTL.Size = new System.Drawing.Size(109, 21); this.NumTTL.TabIndex = 20; // // labelTimeout // this.labelTimeout.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelTimeout.AutoSize = true; this.labelTimeout.Location = new System.Drawing.Point(12, 120); this.labelTimeout.Name = "labelTimeout"; this.labelTimeout.Size = new System.Drawing.Size(53, 12); this.labelTimeout.TabIndex = 3; this.labelTimeout.Text = " Timeout"; // // NumTimeout // this.NumTimeout.ImeMode = System.Windows.Forms.ImeMode.Off; this.NumTimeout.Location = new System.Drawing.Point(71, 116); this.NumTimeout.Maximum = new decimal(new int[] { 60, 0, 0, 0}); this.NumTimeout.Name = "NumTimeout"; this.NumTimeout.Size = new System.Drawing.Size(109, 21); this.NumTimeout.TabIndex = 19; // // DNSText // this.DNSText.ImeMode = System.Windows.Forms.ImeMode.Off; this.DNSText.Location = new System.Drawing.Point(71, 35); this.DNSText.MaxLength = 0; this.DNSText.Name = "DNSText"; this.DNSText.Size = new System.Drawing.Size(109, 21); this.DNSText.TabIndex = 17; this.DNSText.WordWrap = false; // // buttonDefault // this.buttonDefault.Location = new System.Drawing.Point(71, 6); this.buttonDefault.Name = "buttonDefault"; this.buttonDefault.Size = new System.Drawing.Size(109, 23); this.buttonDefault.TabIndex = 16; this.buttonDefault.Text = "Set Default"; this.buttonDefault.UseVisualStyleBackColor = true; this.buttonDefault.Click += new System.EventHandler(this.buttonDefault_Click); // // label2 // this.label2.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(42, 39); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(23, 12); this.label2.TabIndex = 3; this.label2.Text = "DNS"; // // label3 // this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(6, 66); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(59, 12); this.label3.TabIndex = 3; this.label3.Text = "Local DNS"; // // LocalDNSText // this.LocalDNSText.ImeMode = System.Windows.Forms.ImeMode.Off; this.LocalDNSText.Location = new System.Drawing.Point(71, 62); this.LocalDNSText.MaxLength = 0; this.LocalDNSText.Name = "LocalDNSText"; this.LocalDNSText.Size = new System.Drawing.Size(109, 21); this.LocalDNSText.TabIndex = 17; this.LocalDNSText.WordWrap = false; // // SettingsForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ClientSize = new System.Drawing.Size(728, 513); this.Controls.Add(this.tableLayoutPanel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "SettingsForm"; this.Padding = new System.Windows.Forms.Padding(12, 13, 12, 13); this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "SettingsForm"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.SettingsForm_FormClosed); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.tableLayoutPanel2.ResumeLayout(false); this.tableLayoutPanel2.PerformLayout(); this.Socks5ProxyGroup.ResumeLayout(false); this.Socks5ProxyGroup.PerformLayout(); this.tableLayoutPanel9.ResumeLayout(false); this.tableLayoutPanel9.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumS5Port)).EndInit(); this.ListenGroup.ResumeLayout(false); this.ListenGroup.PerformLayout(); this.tableLayoutPanel4.ResumeLayout(false); this.tableLayoutPanel4.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumProxyPort)).EndInit(); this.tableLayoutPanel10.ResumeLayout(false); this.tableLayoutPanel10.PerformLayout(); this.tableLayoutPanel3.ResumeLayout(false); this.tableLayoutPanel5.ResumeLayout(false); this.tableLayoutPanel5.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.NumReconnect)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.NumTTL)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.NumTimeout)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.GroupBox Socks5ProxyGroup; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel9; private System.Windows.Forms.TextBox TextS5Pass; private System.Windows.Forms.TextBox TextS5User; private System.Windows.Forms.Label LabelS5Password; private System.Windows.Forms.Label LabelS5Server; private System.Windows.Forms.Label LabelS5Port; private System.Windows.Forms.TextBox TextS5Server; private System.Windows.Forms.NumericUpDown NumS5Port; private System.Windows.Forms.Label LabelS5Username; private System.Windows.Forms.CheckBox CheckSockProxy; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel10; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; private System.Windows.Forms.Button MyCancelButton; private System.Windows.Forms.Button OKButton; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel5; private System.Windows.Forms.NumericUpDown NumProxyPort; private System.Windows.Forms.Label ProxyPortLabel; private System.Windows.Forms.Label ReconnectLabel; private System.Windows.Forms.NumericUpDown NumReconnect; private System.Windows.Forms.Label TTLLabel; private System.Windows.Forms.NumericUpDown NumTTL; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.Label LabelRandom; private System.Windows.Forms.ComboBox RandomComboBox; private System.Windows.Forms.CheckBox CheckAutoBan; private System.Windows.Forms.CheckBox checkRandom; private System.Windows.Forms.CheckBox checkAutoStartup; private System.Windows.Forms.CheckBox checkShareOverLan; private System.Windows.Forms.ComboBox comboProxyType; private System.Windows.Forms.GroupBox ListenGroup; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; private System.Windows.Forms.TextBox TextAuthPass; private System.Windows.Forms.Label LabelAuthPass; private System.Windows.Forms.TextBox TextAuthUser; private System.Windows.Forms.Label LabelAuthUser; private System.Windows.Forms.CheckBox checkBoxPacProxy; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox TextUserAgent; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox DNSText; private System.Windows.Forms.Label labelTimeout; private System.Windows.Forms.NumericUpDown NumTimeout; private System.Windows.Forms.Button buttonDefault; private System.Windows.Forms.CheckBox checkBalanceInGroup; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox LocalDNSText; } } ================================================ FILE: shadowsocks-csharp/View/SettingsForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; namespace Shadowsocks.View { public partial class SettingsForm : Form { private ShadowsocksController controller; // this is a copy of configuration that we are working on private Configuration _modifiedConfiguration; private Dictionary _balanceIndexMap = new Dictionary(); public SettingsForm(ShadowsocksController controller) { this.Font = System.Drawing.SystemFonts.MessageBoxFont; InitializeComponent(); this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.controller = controller; UpdateTexts(); controller.ConfigChanged += controller_ConfigChanged; int dpi_mul = Util.Utils.GetDpiMul(); //int font_height = 9; //Font new_font = new Font("Arial", (float)(9.0 * dpi_mul / 4)); //this.Font = new_font; //comboProxyType.Height = comboProxyType.Height - font_height + font_height * dpi_mul / 4; comboProxyType.Width = comboProxyType.Width * dpi_mul / 4; //RandomComboBox.Height = RandomComboBox.Height - font_height + font_height * dpi_mul / 4; RandomComboBox.Width = RandomComboBox.Width * dpi_mul / 4; TextS5Server.Width = TextS5Server.Width * dpi_mul / 4; NumS5Port.Width = NumS5Port.Width * dpi_mul / 4; TextS5User.Width = TextS5User.Width * dpi_mul / 4; TextS5Pass.Width = TextS5Pass.Width * dpi_mul / 4; TextUserAgent.Width = TextUserAgent.Width * dpi_mul / 4; NumProxyPort.Width = NumProxyPort.Width * dpi_mul / 4; TextAuthUser.Width = TextAuthUser.Width * dpi_mul / 4; TextAuthPass.Width = TextAuthPass.Width * dpi_mul / 4; buttonDefault.Height = buttonDefault.Height * dpi_mul / 4; buttonDefault.Width = buttonDefault.Width * dpi_mul / 4; DNSText.Width = DNSText.Width * dpi_mul / 4; LocalDNSText.Width = LocalDNSText.Width * dpi_mul / 4; NumReconnect.Width = NumReconnect.Width * dpi_mul / 4; NumTimeout.Width = NumTimeout.Width * dpi_mul / 4; NumTTL.Width = NumTTL.Width * dpi_mul / 4; LoadCurrentConfiguration(); } private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e) { controller.ConfigChanged -= controller_ConfigChanged; } private void UpdateTexts() { this.Text = I18N.GetString("Global Settings") + "(" + (controller.GetCurrentConfiguration().shareOverLan ? "any" : "local") + ":" + controller.GetCurrentConfiguration().localPort.ToString() + I18N.GetString(" Version") + UpdateChecker.FullVersion + ")"; ListenGroup.Text = I18N.GetString(ListenGroup.Text); checkShareOverLan.Text = I18N.GetString(checkShareOverLan.Text); ProxyPortLabel.Text = I18N.GetString("Proxy Port"); ReconnectLabel.Text = I18N.GetString("Reconnect Times"); TTLLabel.Text = I18N.GetString("TTL"); labelTimeout.Text = I18N.GetString(labelTimeout.Text); checkAutoStartup.Text = I18N.GetString(checkAutoStartup.Text); checkRandom.Text = I18N.GetString(checkRandom.Text); CheckAutoBan.Text = I18N.GetString("AutoBan"); Socks5ProxyGroup.Text = I18N.GetString(Socks5ProxyGroup.Text); checkBoxPacProxy.Text = I18N.GetString(checkBoxPacProxy.Text); CheckSockProxy.Text = I18N.GetString("Proxy On"); LabelS5Server.Text = I18N.GetString("Server IP"); LabelS5Port.Text = I18N.GetString("Server Port"); LabelS5Server.Text = I18N.GetString("Server IP"); LabelS5Port.Text = I18N.GetString("Server Port"); LabelS5Username.Text = I18N.GetString("Username"); LabelS5Password.Text = I18N.GetString("Password"); LabelAuthUser.Text = I18N.GetString("Username"); LabelAuthPass.Text = I18N.GetString("Password"); LabelRandom.Text = I18N.GetString("Balance"); for (int i = 0; i < comboProxyType.Items.Count; ++i) { comboProxyType.Items[i] = I18N.GetString(comboProxyType.Items[i].ToString()); } checkBalanceInGroup.Text = I18N.GetString("Balance in group"); for (int i = 0; i < RandomComboBox.Items.Count; ++i) { _balanceIndexMap[i] = RandomComboBox.Items[i].ToString(); RandomComboBox.Items[i] = I18N.GetString(RandomComboBox.Items[i].ToString()); } OKButton.Text = I18N.GetString("OK"); MyCancelButton.Text = I18N.GetString("Cancel"); } private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); } private void ShowWindow() { this.Opacity = 1; this.Show(); } private int SaveOldSelectedServer() { try { int localPort = int.Parse(NumProxyPort.Text); Configuration.CheckPort(localPort); int ret = 0; _modifiedConfiguration.shareOverLan = checkShareOverLan.Checked; _modifiedConfiguration.localPort = localPort; _modifiedConfiguration.reconnectTimes = NumReconnect.Text.Length == 0 ? 0 : int.Parse(NumReconnect.Text); if (checkAutoStartup.Checked != AutoStartup.Check() && !AutoStartup.Set(checkAutoStartup.Checked)) { MessageBox.Show(I18N.GetString("Failed to update registry")); } _modifiedConfiguration.random = checkRandom.Checked; _modifiedConfiguration.balanceAlgorithm = RandomComboBox.SelectedIndex >= 0 && RandomComboBox.SelectedIndex < _balanceIndexMap.Count ? _balanceIndexMap[RandomComboBox.SelectedIndex] : "OneByOne"; _modifiedConfiguration.randomInGroup = checkBalanceInGroup.Checked; _modifiedConfiguration.TTL = Convert.ToInt32(NumTTL.Value); _modifiedConfiguration.connectTimeout = Convert.ToInt32(NumTimeout.Value); _modifiedConfiguration.dnsServer = DNSText.Text; _modifiedConfiguration.localDnsServer = LocalDNSText.Text; _modifiedConfiguration.proxyEnable = CheckSockProxy.Checked; _modifiedConfiguration.pacDirectGoProxy = checkBoxPacProxy.Checked; _modifiedConfiguration.proxyType = comboProxyType.SelectedIndex; _modifiedConfiguration.proxyHost = TextS5Server.Text; _modifiedConfiguration.proxyPort = Convert.ToInt32(NumS5Port.Value); _modifiedConfiguration.proxyAuthUser = TextS5User.Text; _modifiedConfiguration.proxyAuthPass = TextS5Pass.Text; _modifiedConfiguration.proxyUserAgent = TextUserAgent.Text; _modifiedConfiguration.authUser = TextAuthUser.Text; _modifiedConfiguration.authPass = TextAuthPass.Text; _modifiedConfiguration.autoBan = CheckAutoBan.Checked; return ret; } catch (Exception ex) { MessageBox.Show(ex.Message); } return -1; // ERROR } private void LoadSelectedServer() { checkShareOverLan.Checked = _modifiedConfiguration.shareOverLan; NumProxyPort.Value = _modifiedConfiguration.localPort; NumReconnect.Value = _modifiedConfiguration.reconnectTimes; checkAutoStartup.Checked = AutoStartup.Check(); checkRandom.Checked = _modifiedConfiguration.random; int selectedIndex = 0; for (int i = 0; i < _balanceIndexMap.Count; ++i) { if (_modifiedConfiguration.balanceAlgorithm == _balanceIndexMap[i]) { selectedIndex = i; break; } } RandomComboBox.SelectedIndex = selectedIndex; checkBalanceInGroup.Checked = _modifiedConfiguration.randomInGroup; NumTTL.Value = _modifiedConfiguration.TTL; NumTimeout.Value = _modifiedConfiguration.connectTimeout; DNSText.Text = _modifiedConfiguration.dnsServer; LocalDNSText.Text = _modifiedConfiguration.localDnsServer; CheckSockProxy.Checked = _modifiedConfiguration.proxyEnable; checkBoxPacProxy.Checked = _modifiedConfiguration.pacDirectGoProxy; comboProxyType.SelectedIndex = _modifiedConfiguration.proxyType; TextS5Server.Text = _modifiedConfiguration.proxyHost; NumS5Port.Value = _modifiedConfiguration.proxyPort; TextS5User.Text = _modifiedConfiguration.proxyAuthUser; TextS5Pass.Text = _modifiedConfiguration.proxyAuthPass; TextUserAgent.Text = _modifiedConfiguration.proxyUserAgent; TextAuthUser.Text = _modifiedConfiguration.authUser; TextAuthPass.Text = _modifiedConfiguration.authPass; CheckAutoBan.Checked = _modifiedConfiguration.autoBan; } private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfiguration(); LoadSelectedServer(); } private void OKButton_Click(object sender, EventArgs e) { if (SaveOldSelectedServer() == -1) { return; } controller.SaveServersConfig(_modifiedConfiguration); this.Close(); } private void CancelButton_Click(object sender, EventArgs e) { this.Close(); } private void buttonDefault_Click(object sender, EventArgs e) { if (CheckSockProxy.Checked) { NumReconnect.Value = 4; NumTimeout.Value = 10; NumTTL.Value = 60; } else { NumReconnect.Value = 4; NumTimeout.Value = 5; NumTTL.Value = 60; } } } } ================================================ FILE: shadowsocks-csharp/View/SettingsForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/ShowTextForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class ShowTextForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.textBox = new System.Windows.Forms.TextBox(); this.PictureQRcode = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.PictureQRcode)).BeginInit(); this.SuspendLayout(); // // textBox // this.textBox.Dock = System.Windows.Forms.DockStyle.Top; this.textBox.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.textBox.Location = new System.Drawing.Point(0, 0); this.textBox.Multiline = true; this.textBox.Name = "textBox"; this.textBox.Size = new System.Drawing.Size(350, 63); this.textBox.TabIndex = 0; this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged); this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress); // // PictureQRcode // this.PictureQRcode.Dock = System.Windows.Forms.DockStyle.Bottom; this.PictureQRcode.Location = new System.Drawing.Point(0, 68); this.PictureQRcode.Name = "PictureQRcode"; this.PictureQRcode.Size = new System.Drawing.Size(350, 350); this.PictureQRcode.TabIndex = 1; this.PictureQRcode.TabStop = false; // // ShowTextForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.ClientSize = new System.Drawing.Size(350, 418); this.Controls.Add(this.PictureQRcode); this.Controls.Add(this.textBox); this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Name = "ShowTextForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ShowTextForm"; this.TopMost = true; this.SizeChanged += new System.EventHandler(this.ShowTextForm_SizeChanged); ((System.ComponentModel.ISupportInitialize)(this.PictureQRcode)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox; private System.Windows.Forms.PictureBox PictureQRcode; } } ================================================ FILE: shadowsocks-csharp/View/ShowTextForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Properties; using ZXing.QrCode.Internal; namespace Shadowsocks.View { public partial class ShowTextForm : Form { public ShowTextForm(string title, string text) { this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); InitializeComponent(); this.Text = title; PictureQRcode.Height = this.ClientSize.Height - textBox.Height; textBox.Text = text; } private void GenQR(string ssconfig) { int dpi_mul = Util.Utils.GetDpiMul(); int width = Math.Min(PictureQRcode.Width, PictureQRcode.Height) * 4 / 4; try { string qrText = ssconfig; QRCode code = ZXing.QrCode.Internal.Encoder.encode(qrText, ErrorCorrectionLevel.M); ByteMatrix m = code.Matrix; int blockSize = Math.Max(width / (m.Width + 2), 1); Bitmap drawArea = new Bitmap(((m.Width + 2) * blockSize), ((m.Height + 2) * blockSize)); using (Graphics g = Graphics.FromImage(drawArea)) { g.Clear(Color.White); using (Brush b = new SolidBrush(Color.Black)) { for (int row = 0; row < m.Width; row++) { for (int col = 0; col < m.Height; col++) { if (m[row, col] != 0) { g.FillRectangle(b, blockSize * (row + 1), blockSize * (col + 1), blockSize, blockSize); } } } } Bitmap ngnl = Resources.ngnl; int div = 13, div_l = 5, div_r = 8; int l = (m.Width * div_l + div - 1) / div * blockSize, r = (m.Width * div_r + div - 1) / div * blockSize; g.DrawImage(ngnl, new Rectangle(l + blockSize, l + blockSize, r - l, r - l)); } PictureQRcode.Image = drawArea; } catch { } } private void textBox_TextChanged(object sender, EventArgs e) { GenQR(textBox.Text); } private void ShowTextForm_SizeChanged(object sender, EventArgs e) { PictureQRcode.Height = this.ClientSize.Height - textBox.Height; GenQR(textBox.Text); } private void textBox_KeyPress(object sender, KeyPressEventArgs e) { // Use KeyPress to avoid the beep when press Ctrl + A, don't do it in KeyDown if (e.KeyChar == '\x1') { textBox.SelectAll(); e.Handled = true; } } } } ================================================ FILE: shadowsocks-csharp/View/ShowTextForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/View/SubscribeForm.Designer.cs ================================================ namespace Shadowsocks.View { partial class SubscribeForm { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBoxURL = new System.Windows.Forms.TextBox(); this.textBoxGroup = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textUpdate = new System.Windows.Forms.TextBox(); this.checkBoxAutoUpdate = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.buttonOK = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel(); this.listServerSubscribe = new System.Windows.Forms.ListBox(); this.tableLayoutPanel4 = new System.Windows.Forms.TableLayoutPanel(); this.buttonAdd = new System.Windows.Forms.Button(); this.buttonDel = new System.Windows.Forms.Button(); this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); this.tableLayoutPanel3.SuspendLayout(); this.tableLayoutPanel4.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 // this.tableLayoutPanel1.AutoSize = true; this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0); this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1); this.tableLayoutPanel1.Controls.Add(this.textBoxURL, 1, 0); this.tableLayoutPanel1.Controls.Add(this.textBoxGroup, 1, 1); this.tableLayoutPanel1.Controls.Add(this.label3, 0, 2); this.tableLayoutPanel1.Controls.Add(this.textUpdate, 1, 2); this.tableLayoutPanel1.Location = new System.Drawing.Point(529, 4); this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 3; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.Size = new System.Drawing.Size(391, 102); this.tableLayoutPanel1.TabIndex = 0; // // label1 // this.label1.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(60, 7); this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(42, 20); this.label1.TabIndex = 0; this.label1.Text = "URL"; // // label2 // this.label2.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(4, 41); this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(98, 20); this.label2.TabIndex = 0; this.label2.Text = "Group name"; // // textBoxURL // this.textBoxURL.Location = new System.Drawing.Point(110, 4); this.textBoxURL.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.textBoxURL.Name = "textBoxURL"; this.textBoxURL.Size = new System.Drawing.Size(277, 26); this.textBoxURL.TabIndex = 1; // // textBoxGroup // this.textBoxGroup.Location = new System.Drawing.Point(110, 38); this.textBoxGroup.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.textBoxGroup.Name = "textBoxGroup"; this.textBoxGroup.ReadOnly = true; this.textBoxGroup.Size = new System.Drawing.Size(277, 26); this.textBoxGroup.TabIndex = 1; // // label3 // this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(5, 75); this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(97, 20); this.label3.TabIndex = 0; this.label3.Text = "Last Update"; // // textUpdate // this.textUpdate.Location = new System.Drawing.Point(110, 72); this.textUpdate.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.textUpdate.Name = "textUpdate"; this.textUpdate.ReadOnly = true; this.textUpdate.Size = new System.Drawing.Size(277, 26); this.textUpdate.TabIndex = 1; // // checkBoxAutoUpdate // this.checkBoxAutoUpdate.AutoSize = true; this.checkBoxAutoUpdate.Location = new System.Drawing.Point(4, 52); this.checkBoxAutoUpdate.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.checkBoxAutoUpdate.Name = "checkBoxAutoUpdate"; this.checkBoxAutoUpdate.Size = new System.Drawing.Size(123, 24); this.checkBoxAutoUpdate.TabIndex = 3; this.checkBoxAutoUpdate.Text = "Auto update"; this.checkBoxAutoUpdate.UseVisualStyleBackColor = true; // // tableLayoutPanel2 // this.tableLayoutPanel2.Anchor = System.Windows.Forms.AnchorStyles.Bottom; this.tableLayoutPanel2.ColumnCount = 2; this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel2.Controls.Add(this.buttonOK, 0, 0); this.tableLayoutPanel2.Controls.Add(this.buttonCancel, 1, 0); this.tableLayoutPanel2.Location = new System.Drawing.Point(529, 396); this.tableLayoutPanel2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; this.tableLayoutPanel2.RowCount = 1; this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel2.Size = new System.Drawing.Size(435, 90); this.tableLayoutPanel2.TabIndex = 2; // // buttonOK // this.buttonOK.Anchor = System.Windows.Forms.AnchorStyles.None; this.buttonOK.Location = new System.Drawing.Point(32, 16); this.buttonOK.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(153, 58); this.buttonOK.TabIndex = 0; this.buttonOK.Text = "OK"; this.buttonOK.UseVisualStyleBackColor = true; this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click); // // buttonCancel // this.buttonCancel.Anchor = System.Windows.Forms.AnchorStyles.None; this.buttonCancel.Location = new System.Drawing.Point(249, 16); this.buttonCancel.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(153, 58); this.buttonCancel.TabIndex = 0; this.buttonCancel.Text = "Cancel"; this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); // // tableLayoutPanel3 // this.tableLayoutPanel3.ColumnCount = 2; this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 54.17957F)); this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 45.82043F)); this.tableLayoutPanel3.Controls.Add(this.tableLayoutPanel1, 1, 0); this.tableLayoutPanel3.Controls.Add(this.tableLayoutPanel2, 1, 1); this.tableLayoutPanel3.Controls.Add(this.listServerSubscribe, 0, 0); this.tableLayoutPanel3.Controls.Add(this.tableLayoutPanel4, 0, 1); this.tableLayoutPanel3.Location = new System.Drawing.Point(18, 18); this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.tableLayoutPanel3.Name = "tableLayoutPanel3"; this.tableLayoutPanel3.RowCount = 2; this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 78.59327F)); this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 21.40673F)); this.tableLayoutPanel3.Size = new System.Drawing.Size(969, 490); this.tableLayoutPanel3.TabIndex = 1; // // listServerSubscribe // this.listServerSubscribe.FormattingEnabled = true; this.listServerSubscribe.ItemHeight = 20; this.listServerSubscribe.Location = new System.Drawing.Point(4, 4); this.listServerSubscribe.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.listServerSubscribe.Name = "listServerSubscribe"; this.listServerSubscribe.Size = new System.Drawing.Size(512, 364); this.listServerSubscribe.TabIndex = 4; this.listServerSubscribe.SelectedIndexChanged += new System.EventHandler(this.listServerSubscribe_SelectedIndexChanged); // // tableLayoutPanel4 // this.tableLayoutPanel4.ColumnCount = 2; this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.Controls.Add(this.checkBoxAutoUpdate, 0, 1); this.tableLayoutPanel4.Controls.Add(this.buttonAdd, 0, 0); this.tableLayoutPanel4.Controls.Add(this.buttonDel, 1, 0); this.tableLayoutPanel4.Location = new System.Drawing.Point(4, 389); this.tableLayoutPanel4.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.tableLayoutPanel4.Name = "tableLayoutPanel4"; this.tableLayoutPanel4.RowCount = 2; this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel4.Size = new System.Drawing.Size(514, 97); this.tableLayoutPanel4.TabIndex = 5; // // buttonAdd // this.buttonAdd.Anchor = System.Windows.Forms.AnchorStyles.Right; this.buttonAdd.Location = new System.Drawing.Point(141, 7); this.buttonAdd.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.buttonAdd.Name = "buttonAdd"; this.buttonAdd.Size = new System.Drawing.Size(112, 34); this.buttonAdd.TabIndex = 0; this.buttonAdd.Text = "&Add"; this.buttonAdd.UseVisualStyleBackColor = true; this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); // // buttonDel // this.buttonDel.Anchor = System.Windows.Forms.AnchorStyles.Left; this.buttonDel.Location = new System.Drawing.Point(261, 7); this.buttonDel.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.buttonDel.Name = "buttonDel"; this.buttonDel.Size = new System.Drawing.Size(112, 34); this.buttonDel.TabIndex = 1; this.buttonDel.Text = "&Delete"; this.buttonDel.UseVisualStyleBackColor = true; this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click); // // SubscribeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(144F, 144F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ClientSize = new System.Drawing.Size(1022, 546); this.Controls.Add(this.tableLayoutPanel3); this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.Name = "SubscribeForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Subscribe Settings"; this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.SubscribeForm_FormClosed); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.tableLayoutPanel2.ResumeLayout(false); this.tableLayoutPanel3.ResumeLayout(false); this.tableLayoutPanel3.PerformLayout(); this.tableLayoutPanel4.ResumeLayout(false); this.tableLayoutPanel4.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBoxURL; private System.Windows.Forms.TextBox textBoxGroup; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.Button buttonOK; private System.Windows.Forms.Button buttonCancel; private System.Windows.Forms.CheckBox checkBoxAutoUpdate; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3; private System.Windows.Forms.ListBox listServerSubscribe; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel4; private System.Windows.Forms.Button buttonAdd; private System.Windows.Forms.Button buttonDel; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textUpdate; } } ================================================ FILE: shadowsocks-csharp/View/SubscribeForm.cs ================================================ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; namespace Shadowsocks.View { public partial class SubscribeForm : Form { private ShadowsocksController controller; // this is a copy of configuration that we are working on private Configuration _modifiedConfiguration; private int _old_select_index; public SubscribeForm(ShadowsocksController controller) { this.Font = System.Drawing.SystemFonts.MessageBoxFont; InitializeComponent(); this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); this.controller = controller; UpdateTexts(); controller.ConfigChanged += controller_ConfigChanged; LoadCurrentConfiguration(); } private void UpdateTexts() { this.Text = I18N.GetString("Subscribe Settings"); label1.Text = I18N.GetString("URL"); label2.Text = I18N.GetString("Group name"); checkBoxAutoUpdate.Text = I18N.GetString("Auto update"); buttonOK.Text = I18N.GetString("OK"); buttonCancel.Text = I18N.GetString("Cancel"); label3.Text = I18N.GetString("Last Update"); } private void SubscribeForm_FormClosed(object sender, FormClosedEventArgs e) { controller.ConfigChanged -= controller_ConfigChanged; } private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); } private void LoadCurrentConfiguration() { _modifiedConfiguration = controller.GetConfiguration(); LoadAllSettings(); if (listServerSubscribe.Items.Count == 0) { textBoxURL.Enabled = false; } else { textBoxURL.Enabled = true; } } private void LoadAllSettings() { int select_index = 0; checkBoxAutoUpdate.Checked = _modifiedConfiguration.nodeFeedAutoUpdate; UpdateList(); UpdateSelected(select_index); SetSelectIndex(select_index); } private int SaveAllSettings() { _modifiedConfiguration.nodeFeedAutoUpdate = checkBoxAutoUpdate.Checked; return 0; } private void buttonCancel_Click(object sender, EventArgs e) { this.Close(); } private void buttonOK_Click(object sender, EventArgs e) { int select_index = listServerSubscribe.SelectedIndex; SaveSelected(select_index); if (SaveAllSettings() == -1) { return; } controller.SaveServersConfig(_modifiedConfiguration); this.Close(); } private void UpdateList() { listServerSubscribe.Items.Clear(); for (int i = 0; i < _modifiedConfiguration.serverSubscribes.Count; ++i) { ServerSubscribe ss = _modifiedConfiguration.serverSubscribes[i]; listServerSubscribe.Items.Add((String.IsNullOrEmpty(ss.Group) ? " " : ss.Group + " - ") + ss.URL); } } private void SetSelectIndex(int index) { if (index >= 0 && index < _modifiedConfiguration.serverSubscribes.Count) { listServerSubscribe.SelectedIndex = index; } } private void UpdateSelected(int index) { if (index >= 0 && index < _modifiedConfiguration.serverSubscribes.Count) { ServerSubscribe ss = _modifiedConfiguration.serverSubscribes[index]; textBoxURL.Text = ss.URL; textBoxGroup.Text = ss.Group; _old_select_index = index; if (ss.LastUpdateTime != 0) { DateTime now = new DateTime(1970, 1, 1, 0, 0, 0); now = now.AddSeconds(ss.LastUpdateTime); textUpdate.Text = now.ToLongDateString() + " " + now.ToLongTimeString(); } else { textUpdate.Text = "(「・ω・)「"; } } } private void SaveSelected(int index) { if (index >= 0 && index < _modifiedConfiguration.serverSubscribes.Count) { ServerSubscribe ss = _modifiedConfiguration.serverSubscribes[index]; if (ss.URL != textBoxURL.Text) { ss.URL = textBoxURL.Text; ss.Group = ""; ss.LastUpdateTime = 0; } } } private void listServerSubscribe_SelectedIndexChanged(object sender, EventArgs e) { int select_index = listServerSubscribe.SelectedIndex; if (_old_select_index == select_index) return; SaveSelected(_old_select_index); UpdateList(); UpdateSelected(select_index); SetSelectIndex(select_index); } private void buttonAdd_Click(object sender, EventArgs e) { SaveSelected(_old_select_index); int select_index = _modifiedConfiguration.serverSubscribes.Count; if (_old_select_index >= 0 && _old_select_index < _modifiedConfiguration.serverSubscribes.Count) { _modifiedConfiguration.serverSubscribes.Insert(select_index, new ServerSubscribe()); } else { _modifiedConfiguration.serverSubscribes.Add(new ServerSubscribe()); } UpdateList(); UpdateSelected(select_index); SetSelectIndex(select_index); textBoxURL.Enabled = true; } private void buttonDel_Click(object sender, EventArgs e) { int select_index = listServerSubscribe.SelectedIndex; if (select_index >= 0 && select_index < _modifiedConfiguration.serverSubscribes.Count) { _modifiedConfiguration.serverSubscribes.RemoveAt(select_index); if (select_index >= _modifiedConfiguration.serverSubscribes.Count) { select_index = _modifiedConfiguration.serverSubscribes.Count - 1; } UpdateList(); UpdateSelected(select_index); SetSelectIndex(select_index); } if (listServerSubscribe.Items.Count == 0) { textBoxURL.Enabled = false; } } } } ================================================ FILE: shadowsocks-csharp/View/SubscribeForm.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: shadowsocks-csharp/app.config ================================================ ================================================ FILE: shadowsocks-csharp/app.manifest ================================================  True/PM ================================================ FILE: shadowsocks-csharp/shadowsocks-csharp-console.csproj ================================================  Debug AnyCPU 9.0.21022 2.0 {DC8929F9-CAC8-4286-8CFE-6A70EF0EF835} Exe Properties Shadowsocks ShadowsocksR v4.7.1 512 shadowsocks.ico false 3.5 publish\ true Disk false Foreground 7 Days false false true 1 1.0.0.%2a false true app.manifest true bin\Debug\ TRACE;DEBUG;_CONSOLE, PROTOCOL_STATISTICS full AnyCPU prompt ManagedMinimumRules.ruleset false true bin\Release\ TRACE;_CONSOLE, PROTOCOL_STATISTICS true pdbonly AnyCPU prompt ManagedMinimumRules.ruleset false True True Resources.resx ResXFileCodeGenerator Designer Resources.Designer.cs Designer False .NET Framework 3.5 SP1 Client Profile false False .NET Framework 2.0 %28x86%29 false False .NET Framework 3.0 %28x86%29 false False .NET Framework 3.5 true False .NET Framework 3.5 SP1 false False Windows Installer 3.1 true ================================================ FILE: shadowsocks-csharp/shadowsocks-csharp.csproj ================================================  Debug AnyCPU 9.0.21022 2.0 {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} WinExe Properties Shadowsocks ShadowsocksR v4.7.1 512 Shadowsocks.Program shadowsocks.ico false 3.5 publish\ true Disk false Foreground 7 Days false false true 1 1.0.0.%2a false true app.manifest true bin\Debug\ TRACE;DEBUG;PROTOCOL_STATISTICS full AnyCPU prompt ManagedMinimumRules.ruleset false false true bin\Release\ TRACE;PROTOCOL_STATISTICS true pdbonly AnyCPU prompt ManagedMinimumRules.ruleset false True True Resources.resx Form ConfigForm.cs Form InputPassword.cs Form LogForm.cs Form PortSettingsForm.cs Form Form ResetPassword.cs Form ServerLogForm.cs Form SettingsForm.cs Form ShowTextForm.cs Form SubscribeForm.cs ConfigForm.cs Designer ResXFileCodeGenerator Designer Resources.Designer.cs InputPassword.cs LogForm.cs PortSettingsForm.cs ResetPassword.cs ServerLogForm.cs SettingsForm.cs ShowTextForm.cs SubscribeForm.cs Designer False .NET Framework 3.5 SP1 Client Profile false False .NET Framework 2.0 %28x86%29 false False .NET Framework 3.0 %28x86%29 false False .NET Framework 3.5 true False .NET Framework 3.5 SP1 false False Windows Installer 3.1 true ================================================ FILE: shadowsocks-csharp/shadowsocks-csharp4.0.csproj ================================================  Debug AnyCPU 9.0.21022 2.0 {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637} WinExe Properties Shadowsocks ShadowsocksR v4.6.1 512 Shadowsocks.Program shadowsocks.ico false 3.5 publish\ true Disk false Foreground 7 Days false false true 1 1.0.0.%2a false true app.manifest true bin\4.0\Debug\ TRACE;DEBUG;_DOTNET_4_0, PROTOCOL_STATISTICS full AnyCPU prompt ManagedMinimumRules.ruleset false true bin\4.0\Release\ TRACE;_DOTNET_4_0, PROTOCOL_STATISTICS true pdbonly AnyCPU prompt ManagedMinimumRules.ruleset false True True Resources.resx Form ConfigForm.cs Form InputPassword.cs Form LogForm.cs Form PortSettingsForm.cs Form Form ResetPassword.cs Form ServerLogForm.cs Form SettingsForm.cs Form ShowTextForm.cs Form SubscribeForm.cs ConfigForm.cs Designer ResXFileCodeGenerator Designer Resources.Designer.cs InputPassword.cs LogForm.cs PortSettingsForm.cs ResetPassword.cs ServerLogForm.cs SettingsForm.cs ShowTextForm.cs SubscribeForm.cs Designer Designer False .NET Framework 3.5 SP1 Client Profile false False .NET Framework 2.0 %28x86%29 false False .NET Framework 3.0 %28x86%29 false False .NET Framework 3.5 true False .NET Framework 3.5 SP1 false False Windows Installer 3.1 true ================================================ FILE: shadowsocks-csharp/ssr-win-4.0.xml ================================================ ================================================ FILE: shadowsocks-csharp.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27703.2042 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{45913187-0685-4903-B250-DCEF0479CD86}" ProjectSection(ProjectDependencies) = postProject {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637} = {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637} EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp4.0", "shadowsocks-csharp\shadowsocks-csharp4.0.csproj", "{0F2A0C8A-6C06-485B-AA13-AEEC19CA9637}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.Build.0 = Debug|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.ActiveCfg = Release|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.Build.0 = Release|Any CPU {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637}.Release|Any CPU.ActiveCfg = Release|Any CPU {0F2A0C8A-6C06-485B-AA13-AEEC19CA9637}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D7B951DD-1516-479D-A5FA-46E3969EE2E2} EndGlobalSection EndGlobal ================================================ FILE: test/Properties/AssemblyInfo.cs ================================================ using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("test")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("test")] [assembly: AssemblyCopyright("Copyright © 2015")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("f74e87ac-7e3a-444b-a1d9-8b91a674c60f")] // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] ================================================ FILE: test/ServerTest.cs ================================================ using System; using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; using Shadowsocks.Model; using Shadowsocks.Util; namespace test { [TestClass] public class ServerTest { [TestMethod] public void TestServerFromSSR() { Server server = new Server(); string nornameCase = "ssr://MTI3LjAuMC4xOjEyMzQ6YXV0aF9hZXMxMjhfbWQ1OmFlcy0xMjgtY2ZiOnRsczEuMl90aWNrZXRfYXV0aDpZV0ZoWW1KaS8_b2Jmc3BhcmFtPVluSmxZV3QzWVRFeExtMXZaUQ"; server.ServerFromSSR(nornameCase, ""); Assert.AreEqual(server.server, "127.0.0.1"); Assert.AreEqual(server.server_port, 1234); Assert.AreEqual(server.protocol, "auth_aes128_md5"); Assert.AreEqual(server.method, "aes-128-cfb"); Assert.AreEqual(server.obfs, "tls1.2_ticket_auth"); Assert.AreEqual(server.obfsparam, "breakwa11.moe"); Assert.AreEqual(server.password, "aaabbb"); server = new Server(); string normalCaseWithRemark = "ssr://MTI3LjAuMC4xOjEyMzQ6YXV0aF9hZXMxMjhfbWQ1OmFlcy0xMjgtY2ZiOnRsczEuMl90aWNrZXRfYXV0aDpZV0ZoWW1KaS8_b2Jmc3BhcmFtPVluSmxZV3QzWVRFeExtMXZaUSZyZW1hcmtzPTVyV0w2Sy1WNUxpdDVwYUg"; server.ServerFromSSR(normalCaseWithRemark, "firewallAirport"); Assert.AreEqual(server.server, "127.0.0.1"); Assert.AreEqual(server.server_port, 1234); Assert.AreEqual(server.protocol, "auth_aes128_md5"); Assert.AreEqual(server.method, "aes-128-cfb"); Assert.AreEqual(server.obfs, "tls1.2_ticket_auth"); Assert.AreEqual(server.obfsparam, "breakwa11.moe"); Assert.AreEqual(server.password, "aaabbb"); Assert.AreEqual(server.remarks, "测试中文"); Assert.AreEqual(server.group, "firewallAirport"); } [TestMethod] public void TestHideServerName() { Dictionary addrs = new Dictionary(); addrs.Add("127.0.0.1", "127.**.1"); addrs.Add("2001:db8:85a3:8d3:1319:8a2e:370:7348", "2001:**:7348"); addrs.Add("::1319:8a2e:370:7348", "**:7348"); addrs.Add("::1", "**:1"); foreach (string key in addrs.Keys) { string val = ServerName.HideServerAddr(key); Assert.AreEqual(addrs[key], val); } } } } ================================================ FILE: test/UnitTest.cs ================================================ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Shadowsocks.Controller; using Shadowsocks.Encryption; using System.Threading; using System.Collections.Generic; namespace test { [TestClass] public class UnitTest { [TestMethod] public void TestCompareVersion() { Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1.0", "2.3.1") == 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.2", "1.3") < 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.3", "1.2") > 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.3", "1.3") == 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.2.1", "1.2") > 0); Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1", "2.4") < 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.3.2", "1.3.1") > 0); } private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor) { byte[] plain = new byte[16384]; byte[] cipher = new byte[plain.Length + 16]; byte[] plain2 = new byte[plain.Length + 16]; int outLen = 0; int outLen2 = 0; var random = new Random(); random.NextBytes(plain); encryptor.Encrypt(plain, plain.Length, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); Assert.AreEqual(plain.Length, outLen2); for (int j = 0; j < plain.Length; j++) { Assert.AreEqual(plain[j], plain2[j]); } encryptor.Encrypt(plain, 1000, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); Assert.AreEqual(1000, outLen2); for (int j = 0; j < outLen2; j++) { Assert.AreEqual(plain[j], plain2[j]); } encryptor.Encrypt(plain, 12333, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); Assert.AreEqual(12333, outLen2); for (int j = 0; j < outLen2; j++) { Assert.AreEqual(plain[j], plain2[j]); } } private static bool encryptionFailed = false; private static object locker = new object(); [TestMethod] public void TestPolarSSLEncryption() { // run it once before the multi-threading test to initialize global tables RunSinglePolarSSLEncryptionThread(); List threads = new List(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(RunSinglePolarSSLEncryptionThread)); threads.Add(t); t.Start(); } foreach (Thread t in threads) { t.Join(); } Assert.IsFalse(encryptionFailed); } private void RunSinglePolarSSLEncryptionThread() { try { for (int i = 0; i < 100; i++) { IEncryptor encryptor; IEncryptor decryptor; encryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false); decryptor = new MbedTLSEncryptor("aes-256-cfb", "barfoo!", false); RunEncryptionRound(encryptor, decryptor); } } catch { encryptionFailed = true; throw; } } [TestMethod] public void TestRC4Encryption() { // run it once before the multi-threading test to initialize global tables RunSingleRC4EncryptionThread(); List threads = new List(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(RunSingleRC4EncryptionThread)); threads.Add(t); t.Start(); } foreach (Thread t in threads) { t.Join(); } Assert.IsFalse(encryptionFailed); } private void RunSingleRC4EncryptionThread() { try { for (int i = 0; i < 100; i++) { var random = new Random(); IEncryptor encryptor; IEncryptor decryptor; encryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false); decryptor = new MbedTLSEncryptor("rc4-md5", "barfoo!", false); RunEncryptionRound(encryptor, decryptor); } } catch { encryptionFailed = true; throw; } } [TestMethod] public void TestSodiumEncryption() { // run it once before the multi-threading test to initialize global tables RunSingleSodiumEncryptionThread(); List threads = new List(); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread)); threads.Add(t); t.Start(); } foreach (Thread t in threads) { t.Join(); } Assert.IsFalse(encryptionFailed); } private void RunSingleSodiumEncryptionThread() { try { for (int i = 0; i < 100; i++) { var random = new Random(); IEncryptor encryptor; IEncryptor decryptor; encryptor = new SodiumEncryptor("salsa20", "barfoo!", false); decryptor = new SodiumEncryptor("salsa20", "barfoo!", false); RunEncryptionRound(encryptor, decryptor); } } catch { encryptionFailed = true; throw; } } } } ================================================ FILE: test/test.csproj ================================================  Debug AnyCPU {45913187-0685-4903-B250-DCEF0479CD86} Library Properties test test v4.6.1 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest true bin\Debug\ AnyCPU MinimumRecommendedRules.ruleset false bin\Release\ AnyCPU MinimumRecommendedRules.ruleset false {0f2a0c8a-6c06-485b-aa13-aeec19ca9637} shadowsocks-csharp4.0 False False False False