Public Class Arintool
    Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpDownFileName As String) As Integer

    Structure UserINfo
        Dim Auth As Boolean
        Dim Name As String
        Dim IP As String
        Dim Mac As String
        Dim OverDate As Date
        Dim Level As Short
        Dim Admin As Boolean
    End Structure

    Public CurIP As String
    Public CurMac As String
    Public AuthInfo As String
    Public AuthUser As UserINfo

    Public Function GetAuthInfo() As UserINfo
        Try
            Dim UserInfo() As String = GetAuth().Split("%")   '//인증정보를 얻어옵니다

            'MsgBox("getauthuinfo text")

            AuthUser.Auth = CBool(UserInfo(0))
            AuthUser.Name = UserInfo(1)
            AuthUser.IP = UserInfo(2)
            AuthUser.Mac = UserInfo(3)
            AuthUser.OverDate = UserInfo(4)
            AuthUser.Level = CShort(UserInfo(5))
            'AuthUser.Level = 1
            AuthUser.Admin = CBool(UserInfo(6))
            Return AuthUser
        Catch
            AuthUser.IP = CurIP
            AuthUser.Mac = CurMac
            AuthUser.Auth = False
            AuthUser.Name = "정보없음"
            AuthUser.Level = 0
            AuthUser.Admin = False
            Return AuthUser
        End Try
    End Function

    Public Function GetAuth() As String
        Dim Result As String
        Dim TargetFn As String = My.Application.Info.DirectoryPath & "\temp\TempDat.dat"
        CurIP = MyNet.GetLocalHostIP() '//IP도 보내준다.
        CurMac = MyNet.GetMacAddr() '//현재 맥주소를 가져온다
        If Not DownAuthInfo() Then Return vbNullString '//파일이 다운되었다면 인증을 체크한다
        If Not PROC_AUTH() Then Return vbNullString
        If Not Check_UserAuth() Then Return vbNullString
        System.IO.File.Delete(TargetFn)
        Result = AuthUser.Auth & "%" & AuthUser.Name & "%" & AuthUser.IP & "%" & AuthUser.Mac & "%" & AuthUser.OverDate _
        & "%" & AuthUser.Level & "%" & AuthUser.Admin
        'MsgBox(Result)

        Return Result
    End Function

    Private Function Check_UserAuth() As Boolean
        Dim CurDate As Date = Now.Date
        Dim Result As Boolean
        'AuthUser.Auth = False
        Result = IIf(AuthUser.Auth = True AndAlso AuthUser.OverDate >= CurDate AndAlso AuthUser.Level > 0, True, False)
        Return Result
    End Function

    Private Function DownAuthInfo(Optional ByVal DnAddress As String = "http://myhome.naver.com/tindevil/NEWPART2.DAT") As Boolean
        'Dim DnAddress As String = "http://myhome.naver.com/tindevil/NEWPART2.DAT" '//파일명이거 대소문자 가리네;;-_-;
        Dim TargetFn As String = My.Application.Info.DirectoryPath & "\temp\TempDat.dat"
        Dim NetDown As New System.Net.WebClient
        If Not FileIO.FileSystem.DirectoryExists(My.Application.Info.DirectoryPath & "\temp") Then FileIO.FileSystem.CreateDirectory(My.Application.Info.DirectoryPath & "\temp")
        Try
            NetDown.DownloadFile(DnAddress, TargetFn)
            Return FileIO.FileSystem.FileExists(TargetFn)
        Catch
            Return False
        End Try
    End Function

    Private Function PROC_AUTH() As Boolean '//인증과정
        Dim TargetFn As String = My.Application.Info.DirectoryPath & "\temp\TempDat.dat"
        Dim BUF As String
        Dim Cnt As Integer = 0
        Dim FH As Short

        FH = FreeFile()
        FileOpen(FH, TargetFn, OpenMode.Input)
        Do While Not EOF(FH)
            Cnt = Cnt + 1
            BUF = LineInput(FH)

            If Cnt = 1 AndAlso BUF <> "#ARANTOOLAUTH" Then   '//첫줄은 제대로된 파일인지를 검사합니다
                'MsgBox("첫줄실패")
                FileClose(FH)
                Return False
            End If

            If Not (BUF.Trim = "" OrElse BUF.Substring(0, 1) = "#") Then
                If CHECK_AUTH(BUF) Then
                    FileClose(FH)
                    'MsgBox("인증완료됨")
                    Return True
                End If
            End If
        Loop
        FileClose(FH)
        ' MsgBox("사용자없음")
        Return False
    End Function

    Private Function CHECK_AUTH(ByVal INBUF As String) As Boolean
        Dim TempVar() As String = INBUF.Split("|")

        If (TempVar Is Nothing) Or (TempVar.GetUpperBound(0) < 3) Then Return False
        If CurMac Is vbNullString Then Return False

        Try
            If CurMac.ToUpper = TempVar(1).ToUpper Then
                AuthUser.Auth = True
                AuthUser.IP = CurIP
                AuthUser.Mac = CurMac
                AuthUser.Name = TempVar(2)
                AuthUser.OverDate = CType(TempVar(3), Date)
                AuthUser.Level = CShort(TempVar(4))
                If AuthUser.Level = 10 Then AuthUser.Admin = True
                'AuthUser.Level = 1
                Return True
            End If
        Catch
            Return False
        End Try


    End Function


End Class