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