Exemplo n.º 1
0
// GetComputer returns the first match.  It's best to be used for looking up computers by ID or name where you expect exactly one result.
func (svr *lmiServer) GetComputer(c context.Context, req *lmifeedr.ComputerRequest) (*lmifeedr.Computer, error) {
	svr.RLock()
	defer svr.RUnlock()

	computers := svr.logmein.Computers()
	if req.HostID > 0 {
		if c, ok := computers[req.HostID]; ok {
			return convertComputerToGRPC(c), nil
		}
	}
	if len(req.NameMatch) > 0 {
		re, err := regexp.Compile(req.NameMatch)
		if err != nil {
			return nil, err
		}
		for _, c := range computers {
			if re.MatchString(c.Name()) {
				return convertComputerToGRPC(c), nil
			}
		}
	}
	if len(req.IpMatch) > 0 {
		_, ipNet, err := net.ParseCIDR(ensureIPIsCIDR(req.IpMatch))
		if err != nil {
			return nil, err
		}
		for _, c := range computers {
			if ipNet.Contains(c.IPAddress()) {
				return convertComputerToGRPC(c), nil
			}
		}
	}
	return nil, ErrNoMatch
}